網站首頁 編程語言 正文
本文實例為大家分享了C#使用NPOI讀取excel轉為DataSet的具體代碼,供大家參考,具體內容如下
NPOI讀取excel轉為DataSet
////// 讀取Execl數據到DataTable(DataSet)中 /// /// 指定Execl文件路徑 /// 設置第一行是否是列名 ///返回一個DataTable數據集 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; ? ? ? ? ? ? } }
Dataset 導出為Excel
////// 將DataTable(DataSet)導出到Execl文檔 /// /// 傳入一個DataSet /// 導出路徑(可以不加擴展名,不加默認為.xls) ///返回一個Bool類型的值,表示是否導出成功 /// True表示導出成功,Flase表示導出失敗 public static bool DataTableToExcel(DataSet dataSet, string Outpath) ? ? { ? ? ? ? ? ? bool result = false; ? ? ? ? ? ? try ? ? ? ? ? ? { ? ? ? ? ? ? ? ? if (dataSet == null || dataSet.Tables == null || dataSet.Tables.Count == 0 || string.IsNullOrEmpty(Outpath)) ? ? ? ? ? ? ? ? ? ? throw new Exception("輸入的DataSet或路徑異常"); ? ? ? ? ? ? ? ? int sheetIndex = 0; ? ? ? ? ? ? ? ? //根據輸出路徑的擴展名判斷workbook的實例類型 ? ? ? ? ? ? ? ? IWorkbook workbook = null; ? ? ? ? ? ? ? ? string pathExtensionName = Outpath.Trim().Substring(Outpath.Length - 5); ? ? ? ? ? ? ? ? if (pathExtensionName.Contains(".xlsx")) ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? workbook = new XSSFWorkbook(); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? else if(pathExtensionName.Contains(".xls")) ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? workbook = new HSSFWorkbook(); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? else ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? Outpath = Outpath.Trim() + ".xls"; ? ? ? ? ? ? ? ? ? ? workbook = new HSSFWorkbook(); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? //將DataSet導出為Excel ? ? ? ? ? ? ? ? foreach (DataTable dt in dataSet.Tables) ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? sheetIndex++; ? ? ? ? ? ? ? ? ? ? if (dt != null && dt.Rows.Count > 0) ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? ISheet sheet = workbook.CreateSheet(string.IsNullOrEmpty(dt.TableName) ? ("sheet" + sheetIndex) : dt.TableName);//創建一個名稱為Sheet0的表 ? ? ? ? ? ? ? ? ? ? ? ? int rowCount = dt.Rows.Count;//行數 ? ? ? ? ? ? ? ? ? ? ? ? int columnCount = dt.Columns.Count;//列數 ? ? ? ? ? ? ? ? ? ? ? ? //設置列頭 ? ? ? ? ? ? ? ? ? ? ? ? IRow row = sheet.CreateRow(0);//excel第一行設為列頭 ? ? ? ? ? ? ? ? ? ? ? ? for (int c = 0; c < columnCount; c++) ? ? ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ICell cell = row.CreateCell(c); ? ? ? ? ? ? ? ? ? ? ? ? ? ? cell.SetCellValue(dt.Columns[c].ColumnName); ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? //設置每行每列的單元格, ? ? ? ? ? ? ? ? ? ? ? ? for (int i = 0; i < rowCount; i++) ? ? ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? ? ? row = sheet.CreateRow(i + 1); ? ? ? ? ? ? ? ? ? ? ? ? ? ? for (int j = 0; j < columnCount; j++) ? ? ? ? ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ICell cell = row.CreateCell(j);//excel第二行開始寫入數據 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? cell.SetCellValue(dt.Rows[i][j].ToString()); ? ? ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? //向outPath輸出數據 ? ? ? ? ? ? ? ? using (FileStream fs = File.OpenWrite(Outpath)) ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? workbook.Write(fs);//向打開的這個xls文件中寫入數據 ? ? ? ? ? ? ? ? ? ? result = true; ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? return result; ? ? ? ? ? ? } ? ? ? ? ? ? catch (Exception ex) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? return false; ? ? ? ? ? ? } ? ? ? ? } ? ? }
調用方法
DataSet set = ExcelHelper.ExcelToDataTable("test.xlsx", true);//Excel導入 ? bool b = ExcelHelper.DataTableToExcel(set, "test2.xlsx");//導出Excel
原文鏈接:https://blog.csdn.net/qq_42455262/article/details/116191667
相關推薦
- 2022-05-18 React?Hook之使用Effect?Hook的方法_React
- 2022-12-01 sqlserver數據庫導入方法的詳細圖文教程_MsSql
- 2022-12-22 Flutter組件開發過程完整講解_Android
- 2022-11-17 使用python如何實現泛型函數_python
- 2023-07-04 Guava 之 EventBus
- 2023-07-15 element 確認消息的使用步驟
- 2022-09-19 Python+LyScript實現自定義反匯編_python
- 2023-04-10 基于Python實現錄音功能的示例代碼_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支