C#使用NPOI讀取excel轉(zhuǎn)為DataSet
本文實(shí)例為大家分享了C#使用NPOI讀取excel轉(zhuǎn)為DataSet的具體代碼,供大家參考,具體內(nèi)容如下
NPOI讀取excel轉(zhuǎn)為DataSet
/// <summary> /// 讀取Execl數(shù)據(jù)到DataTable(DataSet)中 /// </summary> /// <param name="filePath">指定Execl文件路徑</param> /// <param name="isFirstLineColumnName">設(shè)置第一行是否是列名</param> /// <returns>返回一個(gè)DataTable數(shù)據(jù)集</returns> 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) ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? //循環(huán)讀取Excel的每個(gè)sheet,每個(gè)sheet頁(yè)都轉(zhuǎn)換為一個(gè)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;//獲取總行數(shù) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? if (rowCount > 0) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? IRow firstRow = sheet.GetRow(0);//獲取第一行 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? int cellCount = firstRow.LastCellNum;//獲取總列數(shù) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //構(gòu)建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; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //對(duì)時(shí)間格式(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 導(dǎo)出為Excel
/// <summary> /// 將DataTable(DataSet)導(dǎo)出到Execl文檔 /// </summary> /// <param name="dataSet">傳入一個(gè)DataSet</param> /// <param name="Outpath">導(dǎo)出路徑(可以不加擴(kuò)展名,不加默認(rèn)為.xls)</param> /// <returns>返回一個(gè)Bool類型的值,表示是否導(dǎo)出成功</returns> /// True表示導(dǎo)出成功,F(xiàn)lase表示導(dǎo)出失敗 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; ? ? ? ? ? ? ? ? //根據(jù)輸出路徑的擴(kuò)展名判斷workbook的實(shí)例類型 ? ? ? ? ? ? ? ? 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導(dǎo)出為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);//創(chuàng)建一個(gè)名稱為Sheet0的表 ? ? ? ? ? ? ? ? ? ? ? ? int rowCount = dt.Rows.Count;//行數(shù) ? ? ? ? ? ? ? ? ? ? ? ? int columnCount = dt.Columns.Count;//列數(shù) ? ? ? ? ? ? ? ? ? ? ? ? //設(shè)置列頭 ? ? ? ? ? ? ? ? ? ? ? ? IRow row = sheet.CreateRow(0);//excel第一行設(shè)為列頭 ? ? ? ? ? ? ? ? ? ? ? ? for (int c = 0; c < columnCount; c++) ? ? ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ICell cell = row.CreateCell(c); ? ? ? ? ? ? ? ? ? ? ? ? ? ? cell.SetCellValue(dt.Columns[c].ColumnName); ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? //設(shè)置每行每列的單元格, ? ? ? ? ? ? ? ? ? ? ? ? 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第二行開始寫入數(shù)據(jù) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? cell.SetCellValue(dt.Rows[i][j].ToString()); ? ? ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? //向outPath輸出數(shù)據(jù) ? ? ? ? ? ? ? ? using (FileStream fs = File.OpenWrite(Outpath)) ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? workbook.Write(fs);//向打開的這個(gè)xls文件中寫入數(shù)據(jù) ? ? ? ? ? ? ? ? ? ? result = true; ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? return result; ? ? ? ? ? ? } ? ? ? ? ? ? catch (Exception ex) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? return false; ? ? ? ? ? ? } ? ? ? ? } ? ? }
調(diào)用方法
DataSet set = ExcelHelper.ExcelToDataTable("test.xlsx", true);//Excel導(dǎo)入 ? bool b = ExcelHelper.DataTableToExcel(set, "test2.xlsx");//導(dǎo)出Excel
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- C#利用NPOI操作Excel(單元格設(shè)置)
- C# 基于NPOI操作Excel
- c# 根據(jù)NPOI 讀取一個(gè)excel 文件的多個(gè)Sheet
- C#使用NPOI將excel導(dǎo)入到list的方法
- C#通過NPOI導(dǎo)入導(dǎo)出數(shù)據(jù)EXCEL
- C#用NPOI導(dǎo)出導(dǎo)入Excel幫助類
- C#使用Npoi導(dǎo)出Excel并合并行列
- C#使用NPOI庫(kù)讀寫Excel文件
- C#使用NPOI實(shí)現(xiàn)Excel導(dǎo)入導(dǎo)出功能
- C#使用NPOI設(shè)置Excel下拉選項(xiàng)
相關(guān)文章
C#判斷字符串中是否包含指定字符串及contains與indexof方法效率問題
這篇文章主要介紹了C#判斷字符串中是否包含指定字符串及contains與indexof方法效率問題 ,文中給大家列舉通過兩種方法來判斷,需要的朋友可以參考下2018-10-10C#使用迭代器實(shí)現(xiàn)文字動(dòng)態(tài)效果的示例代碼
這篇文章主要為大家詳細(xì)介紹了C#如何通過使用迭代器實(shí)現(xiàn)文字動(dòng)態(tài)效果,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-02-02C#簡(jiǎn)易圖片格式轉(zhuǎn)換器實(shí)現(xiàn)方法
這篇文章主要介紹了C#簡(jiǎn)易圖片格式轉(zhuǎn)換器實(shí)現(xiàn)方法,涉及C#基于WinForm操作圖片的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-11-11C#操作SQLite數(shù)據(jù)庫(kù)之讀寫數(shù)據(jù)庫(kù)的方法
這篇文章主要介紹了C#操作SQLite數(shù)據(jù)庫(kù)之讀寫數(shù)據(jù)庫(kù)的方法,簡(jiǎn)單分析了C#針對(duì)SQLite數(shù)據(jù)庫(kù)的讀寫及顯示等操作相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2016-07-07C#創(chuàng)建、部署、調(diào)用WebService圖文實(shí)例詳解
本文主要用詳細(xì)的圖文給大家介紹C#創(chuàng)建、部署、調(diào)用WebService的全部過程以及中間需要避免的問題。2017-11-11簡(jiǎn)單對(duì)比C#程序中的單線程與多線程設(shè)計(jì)
這篇文章主要介紹了C#程序中的單線程與多線程設(shè)計(jì)的簡(jiǎn)單對(duì)比,通過實(shí)際的代碼演示可以清晰看出多線程并發(fā)來避免單線程阻塞問題的特點(diǎn),需要的朋友可以參考下2016-04-04