C# Excel To DataTable
2022/1/19 11:25:07
本文主要是介绍C# Excel To DataTable,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
要引用NPOI,引用方法:项目引用那儿右键 => 管理NuGet程序包 => 游览 =>输入NPOI =>选中NPOI后安装(一般是第一个)
/// <summary> /// Excel转DataTable /// </summary> /// <param name="fileName">文件名</param> /// <param name="sheetName">sheet名</param> /// <param name="FirstRowIndex">第一行索引</param> /// <param name="isFirstRowColumn">是否第一行列</param> /// <returns></returns> public System.Data.DataTable ExcelToDataTable(string fileName, string sheetName,int FirstRowIndex, bool isFirstRowColumn) { int startRow = 0; ISheet sheet = null; IWorkbook workbook = null; FileStream fs = null; System.Data.DataTable data = new System.Data.DataTable(); //int startRow = 0; try { fs = new FileStream(fileName, FileMode.Open, FileAccess.Read); if (fileName.IndexOf(".xlsx") > 0) // 2007版本 workbook = new XSSFWorkbook(fs); else if (fileName.IndexOf(".xls") > 0) // 2003版本 workbook = new HSSFWorkbook(fs); if (sheetName != null) { sheet = workbook.GetSheet(sheetName); if (sheet == null) //如果没有找到指定的sheetName对应的sheet,则尝试获取第一个sheet { sheet = workbook.GetSheetAt(0); } } else { sheet = workbook.GetSheetAt(0); } if (sheet != null) { IRow firstRow = sheet.GetRow(FirstRowIndex); //表头第几行开始 int cellCount = firstRow.LastCellNum; //一行最后一个cell的编号 即总的列数 startRow = FirstRowIndex + 1; if (isFirstRowColumn) { for (int i = firstRow.FirstCellNum; i < cellCount; ++i) { ICell cell = firstRow.GetCell(i); if (cell != null) { cell.SetCellType(CellType.String);//Cell.CELL_TYPE_STRING); string cellValue = cell.StringCellValue; if (cellValue != null) { DataColumn column = new DataColumn(cellValue); data.Columns.Add(column); } } } //startRow = sheet.FirstRowNum + 1; } else { // startRow = sheet.FirstRowNum; } //最后一列的标号 int rowCount = sheet.LastRowNum; for (int i = startRow; i <= rowCount; ++i) { IRow row = sheet.GetRow(i); if (row == null) continue; //没有数据的行默认是null if (row.Cells.Count > 2) { if (row.GetCell(0) != null&& row.GetCell(1) != null) { if (row.GetCell(0).ToString() == ""&& row.GetCell(1).ToString() == "") { break; } } else { break; } } else { break; } DataRow dataRow = data.NewRow(); for (int j = row.FirstCellNum; j < cellCount; ++j) { if (j < 0) { continue; } ICell cell = row.GetCell(j); if (row.GetCell(j) != null) //同理,没有数据的单元格都默认是null { // dataRow[j] = row.GetCell(j).ToString(); if (cell.CellType == CellType.Numeric && DateUtil.IsCellDateFormatted(cell)) dataRow[j] = cell.DateCellValue.ToString("yyyy-MM-dd"); else { dataRow[j] = row.GetCell(j).ToString(); } } } data.Rows.Add(dataRow); } } return data; } catch (Exception ex) { MessageBox.Show("Exception: " + ex.Message); return null; } }
这篇关于C# Excel To DataTable的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2022-03-01沐雪多租宝商城源码从.NetCore3.1升级到.Net6的步骤
- 2024-12-06使用Microsoft.Extensions.AI在.NET中生成嵌入向量
- 2024-11-18微软研究:RAG系统的四个层次提升理解与回答能力
- 2024-11-15C#中怎么从PEM格式的证书中提取公钥?-icode9专业技术文章分享
- 2024-11-14云架构设计——如何用diagrams.net绘制专业的AWS架构图?
- 2024-05-08首个适配Visual Studio平台的国产智能编程助手CodeGeeX正式上线!C#程序员必备效率神器!
- 2024-03-30C#设计模式之十六迭代器模式(Iterator Pattern)【行为型】
- 2024-03-29c# datetime tryparse
- 2024-02-21list find index c#
- 2024-01-24convert toint32 c#