Office Excel API (四) Cell

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
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值