using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Interop.Excel;
namespace OfficeUtility
{
/// <summary>
/// Write and read excel cell value
/// </summary>
public class ExcelCellAccessor:IDisposable
{
/// <summary>
/// Two type directions for excel cell accessor
/// </summary>
public enum Direction
{
Down,
Right
}
public Worksheet WorksheetInstance { get; private set; }
public int Row { get; private set; }
public int Col { get; private set; }
public Direction DirectionType { get; private set; }
/// <summary>
/// New ExcelCellAccessor with some parameters setting
/// </summary>
/// <param name="worksheet">The current worksheet which the accessor will work on</param>
/// <param name="row">The row number</param>
/// <param name="col">The column number</param>
/// <param name="direction">The direction where excel accessor will move to</param>
public ExcelCellAccessor(Worksheet worksheet, int row, int col, Direction direction)
{
Reset(worksheet, row, col, direction);
}
/// <summary>
/// Reset position and the direction to the accessor
/// </summary>
/// <param name="worksheet">The worksheet which is working on</param>
/// <param name="row">The row number</param>
/// <param name="col">The column number</param>
/// <param name="direction">The direction where excel accessor will move to</param>
public void Reset(Worksheet worksheet, int row, int col, Direction direction)
{
this.WorksheetInstance = worksheet;
this.Row = row;
this.Col = col;
this.DirectionType = direction;
}
/// <summary>
/// Move accessor to another position
/// </summary>
/// <param name="row">The row number</param>
/// <param name="col">The column number</param>
public void MoveTo(int row, int col)
{
this.Row = row;
this.Col = col;
}
/// <summary>
/// Move the accessor's position, write value then move the accessor to next line or next column
/// </summary>
/// <param name="row">row number</param>
/// <param name="col">col number</param>
/// <param name="line">The content which will be set to value2 of a excel range</param>
public void ResetAndWriteAndMoveNext(int row, int col, object value2)
{
MoveTo(row, col);
WriteAndMoveNext(value2);
}
/// <summary>
/// Move next cell according to the direction: Move down or move right
/// </summary>
public void MoveNext()
{
if (DirectionType == Direction.Down)
{
Row++;
}
else if (DirectionType == Direction.Right)
{
Col++;
}
}
/// <summary>
/// Set "value" to Value2 of a excel range using text format
/// </summary>
/// <param name="value"></param>
public void WriteInPlace(object value)
{
Range range = ExcelRangeUtil.GetRange(Row, Col, this.WorksheetInstance) ;
range.NumberFormat = "@";
range.Value2 = value;
}
/// <summary>
/// Set "value" to Value2 of a excel range using text format and then move next according to the direction, down or right
/// </summary>
/// <param name="value">The value which will be set to the Value2 of a cell using text format</param>
public void WriteAndMoveNext(object value)
{
WriteInPlace(value);
MoveNext();
}
/// <summary>
/// Get Value2 in string and move next according to the direction, down or right
/// </summary>
/// <returns>string format Value2</returns>
public string ReadValue2AndMoveNext()
{
Range range = ExcelRangeUtil.GetRange(Row, Col, this.WorksheetInstance);
if (range.Value2 != null)
{
MoveNext();
return range.Value2.ToString().Trim();
}
else
{
MoveNext();
return string.Empty;
}
}
/// <summary>
/// Get a cell text and move next accoring to the direction, down or right
/// </summary>
/// <returns></returns>
public string ReadTextAndMoveNext()
{
Range range = ExcelRangeUtil.GetRange(Row, Col, this.WorksheetInstance);
if (range.Text != null)
{
MoveNext();
return range.Text.ToString().Trim();
}
else
{
MoveNext();
return string.Empty;
}
}
#region IDisposable Members
public void Dispose()
{
}
#endregion
}
}
Office Excel API (四) Cell
最新推荐文章于 2018-12-18 18:33:07 发布