NOPI導出Excel 自定義列名


NOPI 做Excel 導出確實很方便 ,但是一直在用沒好好研究。

在網上沒找到自定義Columns的方法 ,於是乎自己就在原來的方法上簡單地改改。

 想用的童鞋們可以直接拿去用!

 1         /// 數據大於65536時使用
 2         /// </summary>
 3         /// <param name="dt">數據源</param>
 4         /// <param name="Columns">列名</param>
 5         /// <returns></returns>
 6         public static byte[] ExportToExcel_Columns(DataTable dt, ArrayList Columns)
 7         {
 8             DataColumnCollection str = dt.Columns;
 9             if (str.Count == 0) return null;
10             HSSFWorkbook hssfworkbook;
11             hssfworkbook = new HSSFWorkbook();
12             ISheet sheet1 = hssfworkbook.CreateSheet("Sheet1");
13             List<NPOI.SS.UserModel.ISheet> sheetList = new List<NPOI.SS.UserModel.ISheet>();
14             sheetList.Add(sheet1);
15 
16             int rows = dt.Rows.Count + 1;
17             int p = rows % 65535 == 0 ? rows / 65535 : (rows / 65535) + 1;
18             for (int i = 1; i < p; i++)
19             {
20                 ISheet sheet = hssfworkbook.CreateSheet("sheet" + (i + 1).ToString());
21                 sheetList.Add(sheet);
22             }
23             LargeDataExport_Columns(hssfworkbook, sheetList, dt, Columns);
24             MemoryStream file = new MemoryStream();
25             hssfworkbook.Write(file);
26             file.Close();
27             return file.ToArray();
28         }
29 
30         private static void LargeDataExport_Columns(NPOI.HSSF.UserModel.HSSFWorkbook hssfworkbook, List<NPOI.SS.UserModel.ISheet> sheetCollection, DataTable dt, ArrayList Columns)
31         {
32             // DataColumnCollection str = dt.Columns; //定義表頭,原來是從datatable中獲取的columns
33 
34             for (int i = 0; i < sheetCollection.Count; i++)
35             {
36                 ISheet sheet1 = sheetCollection[i];
37 
38                 if (i == 0)
39                 {
40                     IRow headerRow = sheet1.CreateRow(0);
41                     for (int m = 0, len = Columns.Count; m < len; m++)
42                     {
43                         ICell curCell = headerRow.CreateCell(m);
44                         headerRow.Height = 150 * 3;
45                         ICellStyle style = hssfworkbook.CreateCellStyle();
46                         style.FillPattern = FillPattern.SolidForeground;  
47                         style.FillForegroundColor = HSSFColor.Grey25Percent.LightOrange.Index;
48                         IFont font = hssfworkbook.CreateFont();
49                         font.FontHeightInPoints = 10;
50                         font.Color = HSSFColor.White.Index; //HSSFColor.WHITE.index;
51                         style.SetFont(font);
52                         curCell.CellStyle = style;
53                         curCell.SetCellValue(Columns[m].ToString());
54                         sheet1.SetColumnWidth(m, 400 * 10);
55                     }
56                 }
57 
58                 for (int j = i * 65535; j < (i + 1) * 65535; j++)
59                 {
60                     if (j > dt.Rows.Count - 1)
61                         break;
62                     IRow row = sheet1.CreateRow(j - 65535 * i + 1);
63                     row.Height = 120 * 3;
64 
65                     for (int k = 0; k < dt.Columns.Count; k++)
66                     {
67                         ICell rowCell = row.CreateCell(k);
68                         rowCell.SetCellValue(dt.Rows[j][k].ToString());
69                     }
70                 }
71             }
72         }

 程序調用:

 1         /// <summary>
 2         ///xxxx數據導出
 3         /// </summary>
 4         /// <param name="flg"></param>
 5         /// <returns></returns>
 6         public FileResult ExportExcel_ProfitDetails()
 7         {
 8             DataSet ds = new DataSet();
 9             //獲取當前操作用戶
10             string userRealName = "";
11             if (!string.IsNullOrEmpty(Session["user"].ToString()))
12             {
13                 userRealName = Session["user"].ToString();
14             }
15 
16             tempLog.Info(string.Format("用戶:{0}正在做xxxx數據導出操作", userRealName));
17             string date = Request["datetime"].ToString();
18             ds = Automation.Common.DbHelperSQL.Query("ColligateExport");
19             //創建Excel文件的對象
20             NPOI.HSSF.UserModel.HSSFWorkbook book = new NPOI.HSSF.UserModel.HSSFWorkbook();
21             System.IO.MemoryStream ms = new System.IO.MemoryStream();
22 
23             NPOIExcelHelper n = new NPOIExcelHelper();
24             byte[] fileArr = null;
25             try
26             {
27                 string[] a = new string[] { "ID", "應支付金額", "應支付日期", "支付期數","支付狀態","公司收入","公司支出"
28                   ,"支付類型","工單ID","收益明細ID", "轉讓債權價值","還款金額","還款日期","實際支付金額","實際本金","實際利息"};
29                 ArrayList alist = new ArrayList();
30                 alist.AddRange(a);
31                 fileArr =  NPOIExcelHelper.ExportToExcel_Columns(ds.Tables[0],alist);
32             }
33             catch (Exception ex)
34             {
35                 throw new Exception(ex.Message);
36             }
37             string fileName = "xxxx數據" + System.DateTime.Now.ToString("yyyyMMddhhssmm");
38             tempLog.Info(string.Format("導出xxxx數據的Excel文件名為:{0}", fileName));
39             return File(fileArr, "application/vnd.ms-excel", fileName + ".xls");
40         }

 

最后:使用時記得加上NOPI 引用哦!

轉載請注明出處:http://www.cnblogs.com/apeng/p/5579834.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM