成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

C# 使用 Npoi 操作Excel文件,你會了嗎?

開發 后端
NPOI是指構建在POI 3.x版本之上的一個程序,NPOI可以在沒有安裝Office的情況下對Word或Excel文檔進行讀寫操作。

 [[437708]]

本文轉載自微信公眾號「后端Q」,作者conan。轉載本文請聯系后端Q公眾號。

什么是NPOI

What’s NPOI This project is the .NET version of POI Java project at http://poi.apache.org/. POI is an open source project which can help you read/write xls, doc, ppt files. It has a wide application. For example, you can use it to a. generate a Excel report without Microsoft Office suite installed on your server and more efficient than call Microsoft Excel ActiveX at background; b. extract text from Office documents to help you implement full-text indexing feature (most of time this feature is used to create search engines). c. extract images from Office documents d. generate Excel sheets that contains formulas

在沒有安裝Microsoft Office Excel的機子上也可以對Excel進行操作。另外一種方法是使用.NET自帶的excel API,但是這種方法需要運行環境安裝微軟的excel才行。

C#使用NPOI操作excel

將DataTable數據導入到excel中

  1. /// <summary> 
  2.       /// 將DataTable數據導入到excel中 
  3.       /// </summary> 
  4.       /// <param name="data">要導入的數據</param> 
  5.       /// <param name="isColumnWritten">DataTable的列名是否要導入</param> 
  6.       /// <param name="sheetName">要導入的excel的sheet的名稱</param> 
  7.       /// <returns>導入數據行數(包含列名那一行)</returns
  8.       public int DataTableToExcel(System.Data.DataTable data, string sheetName, bool isColumnWritten) 
  9.       { 
  10.           int i = 0; 
  11.           int j = 0; 
  12.           int count = 0; 
  13.           ISheet sheet = null
  14.  
  15.           try 
  16.           { 
  17.               fs = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite); 
  18.               if (fileName.IndexOf(".xls") > 0) // 2003版本 
  19.                   workbook = new HSSFWorkbook(); 
  20.  
  21.               if (workbook != null
  22.               { 
  23.                   sheet = workbook.CreateSheet(sheetName); 
  24.               } 
  25.               else 
  26.               { 
  27.                   return -1; 
  28.               } 
  29.  
  30.               if (isColumnWritten == true) //寫入DataTable的列名 
  31.               { 
  32.                   IRow row = sheet.CreateRow(0); 
  33.                   for (j = 0; j < data.Columns.Count; ++j) 
  34.                   { 
  35.                       row.CreateCell(j).SetCellValue(data.Columns[j].ColumnName); 
  36.                   } 
  37.                   count = 1; 
  38.               } 
  39.               else 
  40.               { 
  41.                   count = 0; 
  42.               } 
  43.  
  44.               for (i = 0; i < data.Rows.Count; ++i) 
  45.               { 
  46.                   IRow row = sheet.CreateRow(count); 
  47.                   for (j = 0; j < data.Columns.Count; ++j) 
  48.                   { 
  49.                       row.CreateCell(j).SetCellValue(data.Rows[i][j].ToString()); 
  50.                   } 
  51.                   ++count
  52.               } 
  53.               workbook.Write(fs); //寫入到excel 
  54.               return count
  55.           } 
  56.           catch (Exception ex) 
  57.           { 
  58.               Console.WriteLine("Exception: " + ex.Message); 
  59.               return -1; 
  60.           } 
  61.           finally 
  62.           { 
  63.               fs?.Close(); 
  64.           } 
  65.       } 

將excel中的數據導入到DataTable中

  1. /// <summary> 
  2.       /// 將excel中的數據導入到DataTable中 
  3.       /// </summary> 
  4.       /// <param name="sheetName">excel工作薄sheet的名稱</param> 
  5.       /// <param name="isFirstRowColumn">第一行是否是DataTable的列名</param> 
  6.       /// <returns>返回的DataTable</returns
  7.       public System.Data.DataTable ExcelToDataTable(string sheetName, bool isFirstRowColumn) 
  8.       { 
  9.           ISheet sheet = null
  10.           var data = new System.Data.DataTable(); 
  11.           int startRow = 0; 
  12.           try 
  13.           { 
  14.               fs = new FileStream(fileName, FileMode.Open, FileAccess.Read); 
  15.               if (fileName.IndexOf(".xls") > 0) // 2003版本 
  16.                   workbook = new HSSFWorkbook(fs); 
  17.  
  18.               if (sheetName != null
  19.               { 
  20.                   sheet = workbook.GetSheet(sheetName); 
  21.                   if (sheet == null) //如果沒有找到指定的sheetName對應的sheet,則嘗試獲取第一個sheet 
  22.                   { 
  23.                       sheet = workbook.GetSheetAt(0); 
  24.                   } 
  25.               } 
  26.               else 
  27.               { 
  28.                   sheet = workbook.GetSheetAt(0); 
  29.               } 
  30.               if (sheet != null
  31.               { 
  32.                   IRow firstRow = sheet.GetRow(0); 
  33.                   int cellCount = firstRow.LastCellNum; //一行最后一個cell的編號 即總的列數 
  34.                   for (int i = 0; i < cellCount; ++i) 
  35.                   { 
  36.                       var column = new System.Data.DataColumn("column" + i); 
  37.                       data.Columns.Add(column); 
  38.                   } 
  39.                   startRow = sheet.FirstRowNum; 
  40.                   //最后一列的標號 
  41.                   int rowCount = sheet.LastRowNum; 
  42.                   for (int i = startRow; i <= rowCount; ++i) 
  43.                   { 
  44.                       IRow row = sheet.GetRow(i); 
  45.                       if (row == nullcontinue; //沒有數據的行默認是null        
  46.  
  47.                       var dataRow = data.NewRow(); 
  48.                       for (int j = row.FirstCellNum; j < cellCount; ++j) 
  49.                       { 
  50.                           if (row.GetCell(j) != null) //同理,沒有數據的單元格都默認是null 
  51.                               dataRow[j] = row.GetCell(j).ToString(); 
  52.                       } 
  53.                       data.Rows.Add(dataRow); 
  54.                   } 
  55.               } 
  56.  
  57.               return data; 
  58.           } 
  59.           catch (Exception ex) 
  60.           { 
  61.               Console.WriteLine("Exception: " + ex.Message); 
  62.               return null
  63.           } 
  64.       } 

 

 

責任編輯:武曉燕 來源: 后端Q
相關推薦

2024-12-31 00:08:37

C#語言dynamic?

2024-09-10 10:34:48

2025-01-09 07:58:42

C#API函數

2024-12-23 10:06:45

C#深拷貝技術

2024-05-07 07:58:47

C#程序類型

2024-10-16 11:28:42

2024-10-21 07:05:14

C#特性語言

2024-12-12 08:50:30

開源多媒體框架

2021-02-02 07:47:36

NPOI基礎Excel

2024-05-17 08:42:52

AttributeMyClass方法

2024-11-06 11:38:59

C#單例模式

2024-07-03 08:15:39

C#字符串表達式

2025-04-02 08:21:10

2023-06-30 09:45:00

文件讀寫操作Java

2024-02-02 11:03:11

React數據Ref

2023-12-27 07:31:45

json產品場景

2023-10-30 07:05:31

2022-11-08 08:45:30

Prettier代碼格式化工具

2022-11-11 08:29:24

C語言中文字符代碼

2021-02-04 07:22:07

NPOI操作Excel
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 男女视频91 | 欧美性一区二区三区 | 日韩精品视频在线播放 | 久久99国产精一区二区三区 | 国产在线激情视频 | 日韩色综合 | 日日干日日色 | 午夜资源| 中文在线a在线 | 成人不卡| 欧美精品一区二区三 | 一级片在线免费看 | 中文一区 | 福利视频一区二区 | 欧美精品一区二区三区四区五区 | 亚洲人成免费 | 国产成人久久精品一区二区三区 | 草草精品 | 欧美精品一区二区蜜桃 | 久久亚洲精品国产精品紫薇 | 国产一级网站 | 成人免费大片黄在线播放 | 日本精品免费在线观看 | 日本成人在线观看网站 | 久久久久久国产精品mv | 一区二区三区回区在观看免费视频 | 国产精品免费视频一区 | 黄色一级大片在线免费看产 | 超碰97av| 天天操天天怕 | 日韩欧美国产综合 | 天天干夜夜操 | 成人免费在线观看 | 黄色在线免费观看视频 | 国产精品揄拍一区二区 | 伊人久久大香线 | 91九色porny首页最多播放 | 免费视频一区二区三区在线观看 | 久久日韩粉嫩一区二区三区 | 日韩www | 久久精品国产99国产精品 |