c#通用拆分工作表类(支持多列拆分)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Excel = Microsoft.Office.Interop.Excel;
using System.Text.RegularExpressions;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;


namespace CpmBigDataImportService
{
    /// <summary>
    /// 通用拆分工作表类
    /// </summary>
    public class DetachSheetCommon
    {
        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="sht"></param>
        /// <param name="titleStartLine">标题开始行</param>
        /// <param name="titleEndLine">标题结束行</param>
        /// <param name="flagCols">拆分列,int数组</param>
        /// <param name="type">拆分方式 0工作表 1工作簿 2全部</param>
        public DetachSheetCommon(Excel.Application app, Excel.Worksheet sht, int titleStartLine, int titleEndLine, int[] flagCols, int type = 0)
        {
            this.app = app;
            this.sht = sht;
            this.DetachType = type;

            string dimension = sht.UsedRange.Address[false, false];
            string[] addresses = dimension.Split(':');
            string lastCellAddress = addresses[addresses.Length - 1];
            this.endColNum = MyCommon.LetterToInt(GetColumnLetter(lastCellAddress));

            this.TitleStartLine = titleStartLine;
            this.TitleEndLine = titleEndLine;
            this.startLine = titleEndLine + 1;


            this.flagCols = flagCols;

            this.endLine = GetRowNumber(lastCellAddress);

            if (this.endLine <= 1)
                throw new Exception("行数错误!");
        }

        private Excel.Application app;

        /// <summary>
        /// 获取单元格地址字母
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        private string GetColumnLetter(string str)
        {
            return Regex.Replace(str, "[0-9]", "", RegexOptions.IgnoreCase);
        }

        private int GetRowNumber(string str)
        {
            return int.Parse(Regex.Replace(str, "[A-Z]", "", RegexOptions.IgnoreCase));
        }

        /// <summary>
        /// 被拆分工作表
        /// </summary>
        private Excel.Worksheet sht;

        /// <summary>
        /// 被拆分工作表
        /// </summary>
        public Excel.Worksheet Sht
        {
            get { return sht; }
            set { sht = value; }
        }

        /// <summary>
        /// 开始列,默认1
        /// </summary>
        private int startColNum = 1;

        /// <summary>
        /// 结束列 usedrange最后一列
        /// </summary>
        private int endColNum;

        /// <summary>
        /// 开始行
        /// </summary>
        private int startLine;

        /// <summary>
        /// 结束行
        /// </summary>
        private int endLine;

        /// <summary>
        /// 标题 开始行 默认1
        /// </summary>
        public int TitleStartLine { set; get; }

        /// <s
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值