自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

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

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

 [[437708]]

本文轉(zhuǎn)載自微信公眾號(hào)「后端Q」,作者conan。轉(zhuǎn)載本文請(qǐng)聯(lián)系后端Q公眾號(hào)。

什么是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的機(jī)子上也可以對(duì)Excel進(jìn)行操作。另外一種方法是使用.NET自帶的excel API,但是這種方法需要運(yùn)行環(huán)境安裝微軟的excel才行。

C#使用NPOI操作excel

將DataTable數(shù)據(jù)導(dǎo)入到excel中

  1. /// <summary> 
  2.       /// 將DataTable數(shù)據(jù)導(dǎo)入到excel中 
  3.       /// </summary> 
  4.       /// <param name="data">要導(dǎo)入的數(shù)據(jù)</param> 
  5.       /// <param name="isColumnWritten">DataTable的列名是否要導(dǎo)入</param> 
  6.       /// <param name="sheetName">要導(dǎo)入的excel的sheet的名稱</param> 
  7.       /// <returns>導(dǎo)入數(shù)據(jù)行數(shù)(包含列名那一行)</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中的數(shù)據(jù)導(dǎo)入到DataTable中

  1. /// <summary> 
  2.       /// 將excel中的數(shù)據(jù)導(dǎo)入到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對(duì)應(yīng)的sheet,則嘗試獲取第一個(gè)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; //一行最后一個(gè)cell的編號(hào) 即總的列數(shù) 
  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.                   //最后一列的標(biāo)號(hào) 
  41.                   int rowCount = sheet.LastRowNum; 
  42.                   for (int i = startRow; i <= rowCount; ++i) 
  43.                   { 
  44.                       IRow row = sheet.GetRow(i); 
  45.                       if (row == nullcontinue; //沒有數(shù)據(jù)的行默認(rèn)是null        
  46.  
  47.                       var dataRow = data.NewRow(); 
  48.                       for (int j = row.FirstCellNum; j < cellCount; ++j) 
  49.                       { 
  50.                           if (row.GetCell(j) != null) //同理,沒有數(shù)據(jù)的單元格都默認(rèn)是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.       } 

 

 

責(zé)任編輯:武曉燕 來源: 后端Q
相關(guān)推薦

2024-12-31 00:08:37

C#語言dynamic?

2024-09-10 10:34:48

2024-12-23 10:06:45

C#深拷貝技術(shù)

2025-01-09 07:58:42

C#API函數(shù)

2024-05-07 07:58:47

C#程序類型

2024-10-16 11:28:42

2024-10-21 07:05:14

C#特性語言

2024-05-17 08:42:52

AttributeMyClass方法

2024-12-12 08:50:30

開源多媒體框架

2021-02-02 07:47:36

NPOI基礎(chǔ)Excel

2024-07-03 08:15:39

C#字符串表達(dá)式

2024-11-06 11:38:59

C#單例模式

2024-02-02 11:03:11

React數(shù)據(jù)Ref

2025-04-02 08:21:10

2023-06-30 09:45:00

文件讀寫操作Java

2023-10-30 07:05:31

2023-12-27 07:31:45

json產(chǎn)品場景

2022-10-21 13:14:41

lua插件neovim

2019-05-20 16:30:36

PythonMySQL存儲(chǔ)

2022-11-08 08:45:30

Prettier代碼格式化工具
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)