1. 程式人生 > >C#中導出數據到Excel表格中

C#中導出數據到Excel表格中

manage app 獲取 讀取 pen shee 格式 之前 pattern

之前PM交給我一個自動化測試的Case,讓我抓取頁面上的數據到Excel表格中,剛好又接了一個之前人家做的系統, 剛好看到可以用NPOI導數據,就動手試試,成功導出。

由於鄙人比較菜,也比較懶, 怕自己忘記了,今天就總結一下,以防下次用可以參考。

1.要使用NPOI,首先需要在Project中Install NPOI的 Package。

右鍵點擊Project------>Manage NuGet Packages---->Search NPOI----->點擊搜索到的NPOI然後點擊等待安裝完畢。

2. 安裝完成後,你可以在Project的References下看到下面的4個Assembly:

NPOI;
NPOI.OOXML;
NPOI.OpenXml4Net;
NPOI.OpenXmlFormats;

3. 現在就可以在代碼中使用啦。。。

首先引用以下幾個命名空間:

Using NPOI.XSSF.UserModel;
Using NPOI.SS.UserModel;
Using NPOI.HSSF.UserModel;

註:POI讀取Excel有兩中格式,一種是HSSF,另一種是XSSF。

HSSF適用2007以前的版本,XSSF適用2007版本及其以上的。

下面是我在本地測試代碼的主要部分:

技術分享
 1  public class XlsxHelper
 2     {
 3 
 4         public static string CreateXLSFile(DataTable dt)
 5         {
 6             HSSFWorkbook hssfworkbook = new HSSFWorkbook();
 7             ISheet sheet1 = hssfworkbook.CreateSheet("table1");
 8             sheet1.CreateFreezePane(1, 1, 1, 1);
 9
10 for (int i=0;i<10;i++) 11 { 12 sheet1.SetColumnWidth(i, 20 * 256); 13 } 14 15 int rowNum = 0; 16 IRow headRow = sheet1.CreateRow(rowNum); 17 headRow.HeightInPoints = 60; 18 ICellStyle headStyle = hssfworkbook.CreateCellStyle(); 19 headStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center; //居中 20 headStyle.VerticalAlignment = VerticalAlignment.Center;//垂直居中 21 headStyle.WrapText = true; //自動換行 22 /****************寫入列標題*********************/ 23 headRow.CreateCell(0).SetCellValue("ID"); 24 headRow.CreateCell(1).SetCellValue("Name"); 25 headRow.CreateCell(2).SetCellValue("Age"); 26 headRow.CreateCell(3).SetCellValue("Salary"); 27 28 sheet1.GetRow(0).GetCell(0).CellStyle = headStyle; 29 sheet1.GetRow(0).GetCell(1).CellStyle = headStyle; 30 sheet1.GetRow(0).GetCell(2).CellStyle = headStyle; 31 sheet1.GetRow(0).GetCell(3).CellStyle = headStyle; 32 33 /*********************寫入字段值*******************/ 34 ICellStyle style = hssfworkbook.CreateCellStyle(); 35 style.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.Red.Index; 36 style.FillPattern = NPOI.SS.UserModel.FillPattern.Squares; 37 style.FillBackgroundColor = NPOI.HSSF.Util.HSSFColor.Red.Index; 38 for(int kr=0;kr<dt.Rows.Count;kr++) 39 { 40 rowNum++; 41 IRow row = sheet1.CreateRow(rowNum); 42 for(int kc=0;kc<dt.Columns.Count;kc++) 43 { 44 row.CreateCell(kc).SetCellValue(dt.Rows[kr][kc].ToString()); 45 } 46 } 47 48 string fullName = GetFilePath(); 49 50 using (FileStream file1 = new System.IO.FileStream(fullName, FileMode.Create, FileAccess.ReadWrite)) 51 { 52 hssfworkbook.Write(file1); 53 } 54 return fullName; 55 } 56 57 public static string GetFilePath() 58 { 59 string filePath; 60 string ExcelPathFolder =ConfigurationManager.AppSettings["folder"]; 61 string filename = "Excel_" + DateTime.Now.Ticks + ".xls"; 62 63 if (!Directory.Exists(ExcelPathFolder)) //如果Folder不存在,則創建 64 Directory.CreateDirectory(ExcelPathFolder); 65 66 filePath = System.IO.Path.Combine(ExcelPathFolder, filename); 67 68 return filePath; 69 } 70 }
View Code

註: 由於我自己有遇到下面的問題,所有順便提一句,引用了命名空間Using System.Configuration, 但是在代碼中使用ConfigurationManager.AppSettings[""]的時候出現當前上下文不存在ConfigurationManager的錯。記得要在Project下的References中添加System.Configuration.

至於數據來源,可以自己構建或從數據庫中獲取。

如果有問題,希望看到的大神指點。不慎感激。要持續記筆記啦,不能懶啦。 妹子會成為最胖噠。。

註: 看到相關好博客 附地址:http://www.cnblogs.com/luxiaoxun/p/3374992.html

C#中導出數據到Excel表格中