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
c#通用拆分工作表类(支持多列拆分)
最新推荐文章于 2025-05-19 08:59:03 发布