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

C#使用NPOI實(shí)現(xiàn)Excel和DataTable的互轉(zhuǎn)

 更新時(shí)間:2022年02月21日 16:30:50   作者:林深時(shí)見(jiàn)祿  
這篇文章主要為大家詳細(xì)介紹了C#使用NPOI實(shí)現(xiàn)Excel和DataTable的互轉(zhuǎn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

什么是NPOI?

NPOI是指構(gòu)建在POI 3.x版本之上的一個(gè)程序,NPOI可以在沒(méi)有安裝Office的情況下對(duì)Word或Excel文檔進(jìn)行讀寫操作。

NPOI是一個(gè)開(kāi)源的C#讀寫Excel、WORD等微軟OLE2組件文檔的項(xiàng)目。

使用NuGet安裝NPOI

NuGet直接搜索NPOI,目前版本是v2.4.1,將其安裝至項(xiàng)目即可。

安裝完成后,項(xiàng)目會(huì)自動(dòng)為我們添加這4個(gè)引用

同時(shí)還需要在程序中引入NPOI.SS.UserModel;NPOI.XSSF.UserModel;NPOI.HSSF.UserModel;三個(gè)命名空間

廢話不多說(shuō),直接上代碼

DataTable導(dǎo)出Excel

/// <summary>
/// Datable導(dǎo)出成Excel
/// </summary>
/// <param name="dt"></param>
/// <param name="file">導(dǎo)出路徑(包括文件名與擴(kuò)展名)</param>
public static void TableToExcel(DataTable dt, string file)
? ? ? ? {
? ? ? ? ? ? IWorkbook workbook;
? ? ? ? ? ? string fileExt = Path.GetExtension(file).ToLower();
? ? ? ? ? ? if (fileExt == ".xlsx") { workbook = new XSSFWorkbook(); } else if (fileExt == ".xls") { workbook = new HSSFWorkbook(); } else { workbook = null; }
? ? ? ? ? ? if (workbook == null) { return; }
? ? ? ? ? ? ISheet sheet = string.IsNullOrEmpty(dt.TableName) ? workbook.CreateSheet("Sheet1") : workbook.CreateSheet(dt.TableName);
?
? ? ? ? ? ? //表頭 ?
? ? ? ? ? ? IRow row = sheet.CreateRow(0);
? ? ? ? ? ? for (int i = 0; i < dt.Columns.Count; i++)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? ICell cell = row.CreateCell(i);
? ? ? ? ? ? ? ? cell.SetCellValue(dt.Columns[i].ColumnName);
? ? ? ? ? ? }
?
? ? ? ? ? ? //數(shù)據(jù) ?
? ? ? ? ? ? for (int i = 0; i < dt.Rows.Count; i++)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? IRow row1 = sheet.CreateRow(i + 1);
? ? ? ? ? ? ? ? for (int j = 0; j < dt.Columns.Count; j++)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ICell cell = row1.CreateCell(j);
? ? ? ? ? ? ? ? ? ? cell.SetCellValue(dt.Rows[i][j].ToString());
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
?
? ? ? ? ? ? //轉(zhuǎn)為字節(jié)數(shù)組 ?
? ? ? ? ? ? MemoryStream stream = new MemoryStream();
? ? ? ? ? ? workbook.Write(stream);
? ? ? ? ? ? var buf = stream.ToArray();
?
? ? ? ? ? ? //保存為Excel文件 ?
? ? ? ? ? ? using (FileStream fs = new FileStream(file, FileMode.Create, FileAccess.Write))
? ? ? ? ? ? {
? ? ? ? ? ? ? ? fs.Write(buf, 0, buf.Length);
? ? ? ? ? ? ? ? fs.Flush();
? ? ? ? ? ? }
? ? ? ? }

Excel導(dǎo)入DataTable

/// <summary>
/// Excel導(dǎo)入成Datable
/// </summary>
/// <param name="file">導(dǎo)入路徑(包含文件名與擴(kuò)展名)</param>
/// <returns></returns>
public static DataTable ExcelToTable(string file)
? ? ? ? {
? ? ? ? ? ? DataTable dt = new DataTable();
? ? ? ? ? ? IWorkbook workbook;
? ? ? ? ? ? string fileExt = Path.GetExtension(file).ToLower();
? ? ? ? ? ? using (FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read))
? ? ? ? ? ? {
? ? ? ? ? ? ? ? //XSSFWorkbook 適用XLSX格式,HSSFWorkbook 適用XLS格式
? ? ? ? ? ? ? ? if (fileExt == ".xlsx") { workbook = new XSSFWorkbook(fs); } else if (fileExt == ".xls") { workbook = new HSSFWorkbook(fs); } else { workbook = null; }
? ? ? ? ? ? ? ? if (workbook == null) { return null; }
? ? ? ? ? ? ? ? ISheet sheet = workbook.GetSheetAt(0);
?
? ? ? ? ? ? ? ? //表頭 ?
? ? ? ? ? ? ? ? IRow header = sheet.GetRow(sheet.FirstRowNum);
? ? ? ? ? ? ? ? List<int> columns = new List<int>();
? ? ? ? ? ? ? ? for (int i = 0; i < header.LastCellNum; i++)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? object obj = GetValueType(header.GetCell(i));
? ? ? ? ? ? ? ? ? ? if (obj == null || obj.ToString() == string.Empty)
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? dt.Columns.Add(new DataColumn("Columns" + i.ToString()));
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? else
? ? ? ? ? ? ? ? ? ? ? ? dt.Columns.Add(new DataColumn(obj.ToString()));
? ? ? ? ? ? ? ? ? ? columns.Add(i);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? //數(shù)據(jù) ?
? ? ? ? ? ? ? ? for (int i = sheet.FirstRowNum + 1; i <= sheet.LastRowNum; i++)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? DataRow dr = dt.NewRow();
? ? ? ? ? ? ? ? ? ? bool hasValue = false;
? ? ? ? ? ? ? ? ? ? foreach (int j in columns)
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? dr[j] = GetValueType(sheet.GetRow(i).GetCell(j));
? ? ? ? ? ? ? ? ? ? ? ? if (dr[j] != null && dr[j].ToString() != string.Empty)
? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? hasValue = true;
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? if (hasValue)
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? dt.Rows.Add(dr);
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? return dt;
? ? ? ? }
?
? ? ? ? /// <summary>
? ? ? ? /// 獲取單元格類型
? ? ? ? /// </summary>
? ? ? ? /// <param name="cell"></param>
? ? ? ? /// <returns></returns>
? ? ? ? private static object GetValueType(ICell cell)
? ? ? ? {
? ? ? ? ? ? if (cell == null)
? ? ? ? ? ? ? ? return null;
? ? ? ? ? ? switch (cell.CellType)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? case CellType.Blank: //BLANK: ?
? ? ? ? ? ? ? ? ? ? return null;
? ? ? ? ? ? ? ? case CellType.Boolean: //BOOLEAN: ?
? ? ? ? ? ? ? ? ? ? return cell.BooleanCellValue;
? ? ? ? ? ? ? ? case CellType.Numeric: //NUMERIC: ?
? ? ? ? ? ? ? ? ? ? return cell.NumericCellValue;
? ? ? ? ? ? ? ? case CellType.String: //STRING: ?
? ? ? ? ? ? ? ? ? ? return cell.StringCellValue;
? ? ? ? ? ? ? ? case CellType.Error: //ERROR: ?
? ? ? ? ? ? ? ? ? ? return cell.ErrorCellValue;
? ? ? ? ? ? ? ? case CellType.Formula: //FORMULA: ?
? ? ? ? ? ? ? ? default:
? ? ? ? ? ? ? ? ? ? return "=" + cell.CellFormula;
? ? ? ? ? ? }
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • C#用timer實(shí)現(xiàn)背單詞小程序

    C#用timer實(shí)現(xiàn)背單詞小程序

    這篇文章主要為大家詳細(xì)介紹了C#用timer實(shí)現(xiàn)背單詞小程序,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-07-07
  • C# 控件屬性和InitializeComponent()關(guān)系案例詳解

    C# 控件屬性和InitializeComponent()關(guān)系案例詳解

    這篇文章主要介紹了C# 控件屬性和InitializeComponent()關(guān)系案例詳解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-08-08
  • C#圖表算法之有向圖

    C#圖表算法之有向圖

    這篇文章介紹了C#圖表算法之有向圖,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-04-04
  • C#中實(shí)現(xiàn)插入、刪除Excel分頁(yè)符的方法

    C#中實(shí)現(xiàn)插入、刪除Excel分頁(yè)符的方法

    這篇文章主要給大家介紹了關(guān)于在C#中實(shí)現(xiàn)插入、刪除Excel分頁(yè)符的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2018-05-05
  • c# 獲得當(dāng)前絕對(duì)路徑的方法(超簡(jiǎn)單)

    c# 獲得當(dāng)前絕對(duì)路徑的方法(超簡(jiǎn)單)

    下面小編就為大家分享一篇c# 獲得當(dāng)前絕對(duì)路徑的方法(超簡(jiǎn)單),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-01-01
  • C#匿名函數(shù)和匿名方法的使用

    C#匿名函數(shù)和匿名方法的使用

    本文主要介紹了C#匿名函數(shù)和匿名方法的使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-03-03
  • C#優(yōu)雅的實(shí)現(xiàn)INotifyPropertyChanged接口

    C#優(yōu)雅的實(shí)現(xiàn)INotifyPropertyChanged接口

    這篇文章介紹了C#實(shí)現(xiàn)INotifyPropertyChanged接口的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-08-08
  • 利用WinForm實(shí)現(xiàn)上左右布局的方法詳解

    利用WinForm實(shí)現(xiàn)上左右布局的方法詳解

    現(xiàn)在90%的管理系統(tǒng)都是在用上左右這種布局方式,真可謂是經(jīng)典永流傳。本文將利用WinForm實(shí)現(xiàn)上左右布局這一布局效果,感興趣的可以學(xué)習(xí)一下
    2022-09-09
  • Unity實(shí)現(xiàn)人物旋轉(zhuǎn)和移動(dòng)效果

    Unity實(shí)現(xiàn)人物旋轉(zhuǎn)和移動(dòng)效果

    這篇文章主要為大家詳細(xì)介紹了Unity實(shí)現(xiàn)人物旋轉(zhuǎn)和移動(dòng)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-01-01
  • C#中關(guān)于序列化與反序列化的三種方法

    C#中關(guān)于序列化與反序列化的三種方法

    序列化是將對(duì)象的狀態(tài)信息轉(zhuǎn)換為可以存儲(chǔ)或傳輸?shù)男问降倪^(guò)程,本文主要介紹了C#中關(guān)于序列化與反序列化的三種方法,文章具有一定的參考價(jià)值,感興趣的可以了解一下
    2022-03-03

最新評(píng)論

武威市| 通海县| 绥德县| 富宁县| 宁明县| 花莲县| 元谋县| 疏附县| 石河子市| 剑阁县| 兴宁市| 灌云县| 大英县| 永善县| 平阴县| 阿瓦提县| 灌阳县| 咸丰县| 江油市| 石棉县| 乾安县| 陇南市| 柳林县| 永泰县| 阳江市| 剑阁县| 威远县| 秦皇岛市| 桐城市| 罗山县| 阜新市| 平安县| 牟定县| 北京市| 平山县| 新营市| 鹰潭市| 神农架林区| 南澳县| 庆云县| 大新县|