最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C#使用NPOI讀取excel轉(zhuǎn)為DataSet

 更新時(shí)間:2022年02月21日 12:56:07   作者:瘋狂Programmer  
這篇文章主要為大家詳細(xì)介紹了C#使用NPOI讀取excel轉(zhuǎn)為DataSet,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(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;//如果第一行是列名,則從第二行開(kāi)始讀取
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 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第二行開(kāi)始寫入數(shù)據(jù)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? cell.SetCellValue(dt.Rows[i][j].ToString());
? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? //向outPath輸出數(shù)據(jù)
? ? ? ? ? ? ? ? using (FileStream fs = File.OpenWrite(Outpath))
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? workbook.Write(fs);//向打開(kāi)的這個(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í)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 詳解C# parallel中并行計(jì)算的四種寫法總結(jié)

    詳解C# parallel中并行計(jì)算的四種寫法總結(jié)

    在C#中,parallel關(guān)鍵字可以用于并行計(jì)算。本文為大家總結(jié)了四種C# parallel并行計(jì)算的方法,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下
    2022-11-11
  • C#集合之并發(fā)集合的用法

    C#集合之并發(fā)集合的用法

    這篇文章介紹了C#集合之并發(fā)集合的用法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-04-04
  • C#中ToString數(shù)據(jù)類型格式大全(千分符)

    C#中ToString數(shù)據(jù)類型格式大全(千分符)

    這篇文章主要介紹了C#中ToString數(shù)據(jù)類型格式大全 千分符,需要的朋友可以參考下
    2017-02-02
  • 使用C#開(kāi)發(fā)ActiveX控件

    使用C#開(kāi)發(fā)ActiveX控件

    activex控件以前也叫做ole控件,它是微軟ie支持的一種軟件組件或?qū)ο?,可以將其插入到web頁(yè)面中,實(shí)現(xiàn)在瀏覽器端執(zhí)行動(dòng)態(tài)程序功能,以增強(qiáng)瀏覽器端的動(dòng)態(tài)處理能力。通常activex控件都是用c++或vb語(yǔ)言開(kāi)發(fā),本文介紹另一種方式,使用c#語(yǔ)言開(kāi)發(fā)activex控件。
    2017-02-02
  • C#中math類的全部運(yùn)算方法(總結(jié))

    C#中math類的全部運(yùn)算方法(總結(jié))

    下面小編就為大家?guī)?lái)一篇C#中math類的全部運(yùn)算方法(總結(jié))。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2016-09-09
  • C#實(shí)現(xiàn)tostring轉(zhuǎn)換成16進(jìn)制的方法

    C#實(shí)現(xiàn)tostring轉(zhuǎn)換成16進(jìn)制的方法

    本文介紹了在C#中將整數(shù)、字節(jié)數(shù)組、字符串轉(zhuǎn)換為十六進(jìn)制字符串,以及將十六進(jìn)制字符串轉(zhuǎn)換回整數(shù)的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2025-02-02
  • 基于C#編寫一個(gè)接受圖片流的OCR識(shí)別接口

    基于C#編寫一個(gè)接受圖片流的OCR識(shí)別接口

    這篇文章主要為大家詳細(xì)介紹了如何使用C#寫一個(gè)接受圖片流的OCR識(shí)別接口,以及測(cè)試用例調(diào)用接口,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2024-03-03
  • C#異步編程由淺入深(三)之詳解Awaiter

    C#異步編程由淺入深(三)之詳解Awaiter

    這篇文章主要介紹了C#異步編程由淺入深(三)之詳解Awaiter,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-03-03
  • C#實(shí)現(xiàn)自動(dòng)生成電子印章

    C#實(shí)現(xiàn)自動(dòng)生成電子印章

    網(wǎng)絡(luò)辦公正逐漸成為常態(tài),無(wú)紙化辦公也是一個(gè)潮流。本文將利用C#語(yǔ)言實(shí)現(xiàn)自動(dòng)生成電子印章功能,文中的示例代碼講解詳細(xì),感興趣的可以了解一下
    2022-08-08
  • winform綁定快捷鍵的方法

    winform綁定快捷鍵的方法

    這篇文章主要介紹了winform綁定快捷鍵的方法,涉及WinForm設(shè)置窗體按鍵事件的相關(guān)技巧,需要的朋友可以參考下
    2015-05-05

最新評(píng)論

绿春县| 阿拉善右旗| 华坪县| 康平县| 辽阳市| 赤壁市| 舒城县| 新闻| 林州市| 花垣县| 乐业县| 岱山县| 天峨县| 南城县| 临夏县| 广德县| 新泰市| 双桥区| 栾川县| 子长县| 浑源县| 安顺市| 浠水县| 马鞍山市| 慈溪市| 黄浦区| 抚顺县| 西乌| 邯郸市| 乌拉特中旗| 宾阳县| 禹州市| 武陟县| 紫阳县| 阿坝县| 尚志市| 岑溪市| 清丰县| 安岳县| 大港区| 黎川县|