二开案例.NPOI组件读Excel,并转成DataSet
void Main() { var physicalPath = @"D:\test.xlsx"; var dataSet = ExcelToDataSet(physicalPath, true); } public static DataSet ExcelToDataSet(string filePath, bool isFirstLineColumnName) { DataSet dataSet = new DataSet(); int startRow = 0; try { using (FileStream fs = File.OpenRead(filePath)) { IWorkbook workbook = null; // 如果是2007+的Excel版本 if (filePath.IndexOf(".xlsx") > 0) { workbook = new XSSFWorkbook(fs); } // 如果是2003-的Excel版本 else if (filePath.IndexOf(".xls") > 0) { workbook = new HSSFWorkbook(fs); } if (workbook != null) { //循环读取Excel的每个sheet,每个sheet页都转换为一个DataTable,并放在DataSet中 for (int p = 0; p < workbook.NumberOfSheets; p++) { ISheet sheet = workbook.GetSheetAt(p); DataTable dataTable = new DataTable(); dataTable.TableName = sheet.SheetName; if (sheet != null) { int rowCount = sheet.LastRowNum;//获取总行数 if (rowCount > 0) { IRow firstRow = sheet.GetRow(0);//获取第一行 int cellCount = firstRow.LastCellNum;//获取总列数 //构建datatable的列 if (isFirstLineColumnName) { startRow = 1;//如果第一行是列名,则从第二行开始读取 for (int i = firstRow.FirstCellNum; i < cellCount; ++i) { ICell cell = firstRow.GetCell(i); if (cell != null) { if (cell.StringCellValue != null) { DataColumn column = new DataColumn(cell.StringCellValue); dataTable.Columns.Add(column); } } } } else { for (int i = firstRow.FirstCellNum; i < cellCount; ++i) { DataColumn column = new DataColumn("column" + (i + 1)); dataTable.Columns.Add(column); } } //填充行 for (int i = startRow; i <= rowCount; ++i) { IRow row = sheet.GetRow(i); if (row == null) continue; DataRow dataRow = dataTable.NewRow(); for (int j = row.FirstCellNum; j < cellCount; ++j) { ICell cell = row.GetCell(j); if (cell == null) { dataRow[j] = ""; } else { //CellType(Unknown = -1,Numeric = 0,String = 1,Formula = 2,Blank = 3,Boolean = 4,Error = 5,) switch (cell.CellType) { case CellType.Blank: dataRow[j] = ""; break; case CellType.Numeric: short format = cell.CellStyle.DataFormat; //对时间格式(2015.12.5、2015/12/5、2015-12-5等)的处理 if (format == 14 || format == 22 || format == 31 || format == 57 || format == 58) dataRow[j] = cell.DateCellValue; else dataRow[j] = cell.NumericCellValue; break; case CellType.String: dataRow[j] = cell.StringCellValue; break; } } } dataTable.Rows.Add(dataRow); } } } dataSet.Tables.Add(dataTable); } } } return dataSet; } catch (Exception ex) { var msg = ex.Message; return null; } }
楼主能否分享下读xml文档的示例,谢谢
二开案例.NPOI组件读Excel,并转成DataSet
void Main(){ var physicalPath = @"D:\test.xlsx"; var dataSet = ExcelToDataSet(physicalPath, true);}public static DataSet ExcelToDa...
点击下载文档
本文2024-09-16 18:27:24发表“云星空知识”栏目。
本文链接:https://wenku.my7c.com/article/kingdee-k3cloud-22260.html
您需要登录后才可以发表评论, 登录登录 或者 注册
最新文档
热门文章