您现在的位置是:亿华云 > 数据库
NPOI操作Excel之三解析Excel
亿华云2025-10-03 02:27:45【数据库】8人已围观
简介通过前面两篇的基础学习,我们对NPOI有了一定了了解,下面就开始进入实战,解析下面格式的Excel(下面只是列举了几个例子),并保存入库首先我们先分析一下,要解析这样的Excel,需要把指标【橘色背景
通过前面两篇的操作基础学习,我们对NPOI有了一定了了解,解析下面就开始进入实战,操作解析下面格式的解析Excel(下面只是列举了几个例子),并保存入库
首先我们先分析一下,操作要解析这样的解析Excel,需要把指标【橘色背景和蓝色背景】(作为指标入库)、操作科目【棕色背景和黄色背景】(作为X轴入库)、解析数据【乳白色背景和白色背景】(作为Y轴入库)的操作数据分开入库。
第一张图我们得到的解析指标毫无疑问应该是第三行从第二列开始到最后一列的数据,而第二张图我们得到的操作指标应该是非金融企业部门-使用、非金融企业部门-来源、解析金融机构部门-使用、操作金融机构部门-来源,解析以此类推,操作我们要想取到这样的数据,首先需要把合并行的单元格填充、然后把合并列的数据合并,我们可以通过二维数组来实实现。
由于每个Excel的格式不一样,云服务器提供商指标数据的行数,列数也不一样,所以我们要想把数据区分开只能通过背景颜色,把三部分是数据分开并放到三个二维数组里,然后解析入库,由于Excel的背景颜色存在不一样,所以不能写死,通过观察我们可以发现,每个Excel都是从指标行开始有背景颜色到数据行开始变背景颜色,这样我们就可以区分开来,到这里相信聪明的你已经知道怎么做了,下面我们就开始实现吧
1、获取Excel的扩展名并创建工作簿,如果是xls创建HSSFWorkbook工作簿,如果是xlxs创建XSSFWorkbook工作簿
public static void ReadFromExcelFile(string filePath) { IWorkbook wk = null; string extension = System.IO.Path.GetExtension(filePath);//GetExtension获取Excel的扩展名 try { FileStream fs = File.OpenRead(filePath); if (extension.Equals(".xls")) { wk = new HSSFWorkbook(fs); //把xls文件中的数据写入wk中 } else { wk = new XSSFWorkbook(fs);//把xlsx文件中的数据写入wk中 } fs.Close(); sheet = wk.GetSheetAt(0);//读取当前表数据 20 GetIndexRow();//获取【指标、科目、数据】的行数列数 ReadData();//读数据并保存到数组中 SaveData();//解析数组数据并保存入库 } catch (Exception e) { Console.WriteLine(e.Message); //只在Debug模式下才输出 }2、获取指标从哪行开始
for (int i = 0; i < sheet.LastRowNum; i++)//sheet.LastRowNum当前表的行数 { IRow row = sheet.GetRow(i); //读取当前行数据 if (row != null) { if (row.GetCell(0) != null) //读取该行的第1列数据 { ICellStyle style = row.GetCell(0).CellStyle;//当前行第一列的样式 row.GetCell(0).SetCellType(CellType.String);//把第一行第一列的源码库值类型转换成string类型 short GroundColor = style.FillForegroundColor;//获取当前行第一列的背景色 if (i == 0)//若或i=0说明是第一行,没有背景色的 { Title = row.GetCell(0).StringCellValue;//获取第一行第一列的值即标题的值 TitleColor = GroundColor;//第一行第一列背景色的值付给TitleColor continue; } else//如果不是第一行 { if (GroundColor == TitleColor) { if (row.GetCell(0).StringCellValue.Contains("单位")) { IndexUnit = row.GetCell(0).StringCellValue.Replace("单位:", "").Replace("单位:", ""); continue; } } else if (GroundColor != TitleColor && IndexColor == 0)//如果GroundColor不等于TitleColor说明改行是指标行 { IndexColor = GroundColor;// 把GroundColor的值赋值给IndexColor IndexStart = i;//记录改行,改行是指标行的起始行 break; } } } }3、获取指标从哪行结束
for (int i = IndexStart + 1; i < sheet.LastRowNum; i++) { IRow row = sheet.GetRow(i); //读取当前行数据 if (row != null) { if (row.GetCell(0) != null) //读取该行的第1列数据 { ICellStyle style = row.GetCell(0).CellStyle; short GroundColor = style.FillForegroundColor; if (IndexColor != GroundColor) { LeftDataColor = GroundColor; IndexEnd = i - 1; break; } } } }4、获取数据从哪行开始到哪行结束
for (int i = IndexEnd + 1; i < sheet.LastRowNum; i++) { DataRowStart = IndexEnd + 1;//数据开始行 IRow row = sheet.GetRow(i); //读取当前行数据 if (row != null) { if (row.GetCell(0) != null) //读取该行的第1列数据 { ICellStyle style = row.GetCell(0).CellStyle; short GroundColor = style.FillForegroundColor; if (LeftDataColor != GroundColor) { DataRowEnd = i - 1;//数据结束行 break; } } } }5、获取科目【左侧】的列数
if (sheet.GetRow(IndexEnd + 1) != null) { for (int i = 0; i < sheet.GetRow(IndexEnd + 1).LastCellNum; i++) { if (sheet.GetRow(IndexEnd + 1).GetCell(i) != null) { ICellStyle style = sheet.GetRow(IndexEnd + 1).GetCell(i).CellStyle; short GroundColor = style.FillForegroundColor; sheet.GetRow(IndexEnd + 1).GetCell(i).SetCellType(CellType.String); if (GroundColor != LeftDataColor) { DataLeftCell = i;//科目的列数 break; } } } }6、把数据保存到数组中【指标数组】
string[,] IndexArray = new string[IndexEnd-IndexStart+1, sheet.GetRow(0).LastCellNum - DataLeftCell];//指标 4 //循环指标行 for (int r = IndexStart; r <= IndexEnd; r++) { IRow row = sheet.GetRow(r); //读取当前行数据 if (row != null) { for (int c = DataLeftCell; c <= row.LastCellNum - DataLeftCell; c++) { if (row.GetCell(c) != null) { row.GetCell(c).SetCellType(CellType.String); #region 判断是否是合并单元格 if (string.IsNullOrEmpty(row.GetCell(c).StringCellValue)) { ICell cell = row.GetCell(c); Dimension dimension = new Dimension(); if (IsMergedRegions.IsMergeCell(cell, out dimension))//如果是空判断是否是合并单元格 { IndexArray[r - IndexStart, c- DataLeftCell] = dimension.DataCell.StringCellValue;//如果是取合并单元格的值 } else { IndexArray[r - IndexStart, c- DataLeftCell] = row.GetCell(c).StringCellValue;//否则取改单元格本身的值 } } else { IndexArray[r - IndexStart, c- DataLeftCell] = row.GetCell(c).StringCellValue; } #endregion } } } }7、把数据保存到数组中【科目数组】
string[,] LeftDataArray = new string[DataRowEnd-DataRowStart+1, DataLeftCell];//科目 for (int r = DataRowStart; r <= DataRowEnd; r++) { IRow row = sheet.GetRow(r); //读取当前行数据 if (row != null) { for (int c = 0; c < DataLeftCell; c++) { if (row.GetCell(c) != null) { row.GetCell(c).SetCellType(CellType.String); #region 判断是否是合并单元格 if (string.IsNullOrEmpty(row.GetCell(c).StringCellValue)) { ICell cell = row.GetCell(c); Dimension dimension = new Dimension(); if (IsMergedRegions.IsMergeCell(cell, out dimension)) { LeftDataArray[r - DataRowStart, c] = dimension.DataCell.StringCellValue; } else { LeftDataArray[r - DataRowStart, c] = row.GetCell(c).StringCellValue; } } else { LeftDataArray[r - DataRowStart, c] = row.GetCell(c).StringCellValue; } #endregion } } } }8、云服务器把数据保存到数组中【数据数组】
string[,] RightDataArray= new string[DataRowEnd - DataRowStart + 1, sheet.GetRow(0).LastCellNum - DataLeftCell];//数据 for (int r = DataRowStart; r <= DataRowEnd; r++) { IRow row = sheet.GetRow(r); //读取当前行数据 if (row != null) { for (int c = DataLeftCell; c < row.LastCellNum; c++) { if (row.GetCell(c) != null) { row.GetCell(c).SetCellType(CellType.String); RightDataArray[r - DataRowStart, c- DataLeftCell] = row.GetCell(c).StringCellValue; } } } }9、解析数组保存数据
private static void SaveData() { //IndexModel im = new IndexModel(); DataModel dm = new DataModel(); for (int ic = 0; ic < sheet.GetRow(0).LastCellNum - DataLeftCell ; ic++)//循环指标列 { dm.IndexName = null; dm.IndexCode = IndexCode++.ToString().PadLeft(4, 0); #region 获取指标名称 for (int ir = 0; ir < IndexEnd - IndexStart + 1; ir++) { if (IndexArray[ir, ic] != null) { if (dm.IndexName == null) { dm.IndexName = IndexArray[ir, ic]; } else { if (!dm.IndexName.Contains(IndexArray[ir, ic])) { dm.IndexName = dm.IndexName + "_" + IndexArray[ir, ic];//同一列字符串拼接 } } } } #endregion //循环得右侧数据 for (int rr = 0; rr < DataRowEnd - DataRowStart + 1; rr++)//循环右侧数据的行 { #region 右侧数据 if (RightDataArray[rr, ic] != null) { dm.IndexYValue = RightDataArray[rr, ic]; } #endregion dm.IndexXValue = null; //循环得左侧数据 for (int lc = 0; lc < DataLeftCell; lc++) { if (LeftDataArray[rr, lc] !=null) { if (dm.IndexXValue == null) { dm.IndexXValue = LeftDataArray[rr, lc]; } else { if (!dm.IndexXValue.Contains(LeftDataArray[rr, lc])) { dm.IndexXValue = dm.IndexXValue + "_" + LeftDataArray[rr, lc]; } } } } Console.WriteLine($"指标名称:{ dm.IndexName} 指标编码:{ dm.IndexCode} IndexXValue:{ dm.IndexXValue} IndexYValue:{ dm.IndexYValue}"); } } }10、上面用到的方法IsMergeCell判断是否是合并单元格
/// <summary> /// 判断指定单元格是否为合并单元格,并且输出该单元格的维度 /// </summary> /// <param name="cell">单元格</param> /// <param name="dimension">单元格维度</param> /// <returns>返回是否为合并单元格的布尔(Boolean)值</returns> public static bool IsMergeCell(this ICell cell, out Dimension dimension) { return cell.Sheet.IsMergeCell(cell.RowIndex, cell.ColumnIndex, out dimension); }https://www.cnblogs.com/zqyw/category/1070314.html
本文转载自微信公众号「CSharp编程大全」,可以通过以下二维码关注。转载本文请联系CSharp编程大全公众号。
很赞哦!(65446)