成人免费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

2024-12-23 10:06:45

C#深拷貝技術

2025-01-09 07:58:42

C#API函數

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基礎Excel

2024-11-06 11:38:59

C#單例模式

2024-07-03 08:15:39

C#字符串表達式

2024-02-02 11:03:11

React數據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產品場景

2022-10-21 13:14:41

lua插件neovim

2019-05-20 16:30:36

PythonMySQL存儲

2022-11-08 08:45:30

Prettier代碼格式化工具
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 亚洲精品一区二三区不卡 | 青青草原综合久久大伊人精品 | 成人国产综合 | 国产一级电影在线观看 | 国产特黄一级 | 伊人免费观看视频 | 日韩欧美在线视频 | 美女黄色在线观看 | 九九视频在线观看 | 久久久美女 | 青青草综合 | 久久久www | 91国内外精品自在线播放 | 亚洲高清在线 | 蜜桃视频在线观看www社区 | 一本色道精品久久一区二区三区 | 精品欧美一区二区中文字幕视频 | 国产在线视频一区 | 欧美国产精品 | 一区二区三区四区av | 亚洲精品区| 黄色一级视频免费 | 午夜精品久久 | 综合激情久久 | 久久国产精品一区二区 | 欧美一级片免费看 | 国产视频久久 | 99中文字幕 | 狠狠干影院 | 久久精品成人 | 伊人网站视频 | av毛片在线| 99精品久久久国产一区二区三 | 久久精品99国产精品 | 欧美三级视频在线观看 | 日韩中文字幕在线视频 | 国产精品一级在线观看 | 91高清视频在线观看 | 一区二区免费高清视频 | 国产精品美女久久久久久久网站 | 国产中文字幕在线观看 |