匯出到Excel中NPOI

2021-09-25 23:42:11 字數 4455 閱讀 7729

1、npoi

感謝tony qu分享出npoi元件的使用方法

3、.net呼叫npoi元件匯入匯出excel的操作類

此npoi操作類的優點如下:

(1)支援web及winform從datatable匯出到excel; 

(2)生成速度很快; 

(3)準確判斷資料型別,不會出現身份證轉數值等問題; 

(4)如果單頁條數大於65535時會新建工作表; 

(5)列寬自適應;

npoi操作類

1 using system;

2 using system.data;

3 using system.configuration;

4 using system.web;

5 using system.web.security;

6 using system.web.ui;

7 using system.web.ui.htmlcontrols;

8 using system.web.ui.webcontrols;

9 using system.web.ui.webcontrols.webparts;

10 using system.io;

11 using system.text;

12 using npoi;

13 using npoi.hpsf;

14 using npoi.hssf;

15 using npoi.hssf.usermodel;

16 using npoi.hssf.util;

17 using npoi.poifs;

18 using npoi.util;

20 namespace pms.common

21 40             }

41         }

42 43         /// 

44         /// datatable匯出到excel的memorystream

45         /// 

46         /// 源datatable

47         /// 表頭文字

48        public static memorystream export(datatable dtsource,string strheadertext)

49        

69            #endregion

70 71             hssfcellstyle datestyle= workbook.createcellstyle();

72             hssfdataformat format= workbook.createdataformat();

73             datestyle.dataformat= format.getformat("yyyy-mm-dd");

74 75            //取得列寬

76            int arrcolwidth= newint[dtsource.columns.count];

77            foreach (datacolumn itemin dtsource.columns)

78            

81            for (int i= 0; i

82            

90                 }

91             }

95            int rowindex = 0;

97            foreach (datarow rowin dtsource.rows)

98            

106 

107                    #region 表頭及樣式

108                    

123                    #endregion

124 

125 

126                    #region 列頭及樣式

127                    

148                         headerrow.dispose();

149                     }

150                    #endregion

151 

152                     rowindex= 2;

153                 }

154                #endregion

155 

156 

157                #region 填充內容

158                 hssfrow datarow= sheet.createrow(rowindex);

159                foreach (datacolumn columnin dtsource.columns)

160                

203 

204                 }

205                #endregion

206 

207                 rowindex++;

208             }

211            using (memorystream ms= new memorystream())

212            

222         }

223 

224         /// 

225         /// 用於web匯出

226         /// 

227         /// 源datatable

228         /// 表頭文字

229         /// 檔名

230        public static void exportbyweb(datatable dtsource,string strheadertext,stringstrfilename)

231        

247 

248         /// 讀取excel

249         /// 預設第一行為標頭

250         /// 

251         /// excel文件路徑

252         /// 

253        public static datatable import(string strfilename)

254        

262             hssfsheet sheet= hssfworkbook.getsheetat(0);

263             system.collections.ienumerator rows= sheet.getrowenumerator();

264 

265             hssfrow headerrow= sheet.getrow(0);

266            int cellcount= headerrow.lastcellnum;

267 

268            for (int j= 0; j

269            

273 

274            for (int i= (sheet.firstrownum+ 1); i<= sheet.lastrownum; i++)

275            

284 

285                 dt.rows.add(datarow);

286             }

287            return dt;

288         }

289     }

290 }

291

4、npoi操作類的呼叫方法

datatable dt_grade = tools.data.getdatatable(strsql);

filename += "績效考核結果分數統計表";

pms.common.npoihelper.exportbyweb(dt_grade, filename, filename+".xls");

5、效果展示

c 使用NPOI匯出到excel

1 新增程式包。在專案名右鍵。選擇管理nuget程式包,瀏覽處搜尋npoi並安裝。2 引用。using system.io using system.data.sqlclient using system.diagnostics using npoi.hssf.usermodel using npo...

利用NPOI匯出到Excel2007

專案中匯出到excel2003中遇到瓶頸,因為列表資料已經超過了65535行,excel2003乙個工作表中最大只支援65535行,解決的辦法有兩個 1,當資料超過65535行時,新建另乙個工作表,把資料寫入另外的工作表中,下面來介紹一下匯出npoi 1.2.4 版本 匯出到excel2003和np...

C 使用NPOI快速匯出到Excel

接上篇博文 c 快速匯出到excel 由於此種方法不能匯出成.xlsx格式,為解決此問題,本次分享使用npoi。參考 1 新增程式包。在專案名右鍵。選擇管理nuget程式包,瀏覽處搜尋npoi並安裝。2 引用。using system.io using system.data.sqlclient u...