之前隨筆使用的是1.x的比較古老的版本了,已經不再維護,接下來使用較新的2.x的版本進行導出
之前一直按照其他的博客與官網的隨筆進行導出,發現一直報錯,后面更換了POI的版本為3.16(因為jxls也是使用的最新的版本),就解決了jar包的沖突問題
jar沖突參考的是:http://www.cnblogs.com/mvilplss/p/6101676.html
一、概述
2.x更新后,比較大的不同是模板,之前的模板是直接將標簽寫在模板中:
新版的模板使用的是批注(單元格右鍵——插入批注即可)的形式(開始不熟悉excel,還找了半天這是啥):
還有xml這種稍微不那么直觀的形式待后續更新
更多的概述介紹可以參見官網:http://jxls.sourceforge.net/
入門程序參考:http://www.cnblogs.com/klguang/p/6425422.html
二、HelloWorld入門
依賴直接根據官網起步提示進行引入:
<dependency>
<groupId>org.jxls</groupId>
<artifactId>jxls</artifactId>
<version>2.4.1</version>
</dependency>
//若實際maven項目中采用此依賴,請將版本分離為properties統一管理
當然,還要的依賴包括(NosuchMethod引起的異常請檢查依賴的沖突,版本的問題)
依賴的介紹可以參見官網:
<dependency>
<groupId>org.jxls</groupId>
<artifactId>jxls-poi</artifactId>
<version>1.0.13</version>
</dependency>
<dependency>
<groupId>org.jxls</groupId>
<artifactId>jxls-jexcel</artifactId>
<version>1.0.6</version>
</dependency>
由於jxls使用的是最新的版本,這里建議POI也調成新的版本(經測試:2.4.1配3.16完美使用)
<poi.version>3.16</poi.version>
<!-- poi office -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>${poi.version}</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>${poi.version}</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>${poi.version}</version>
</dependency>
//jar有待測試具體應該引入哪些版本可以兼容,哪些jar是最小引入
項目結構如下:
//其中,測試類位於上,模板文件位於下(關於這里模板文件的讀取,請參見:http://www.cnblogs.com/jiangbei/p/6744882.html)
這里存在一些命名與規范等問題,實際使用時請規范相關命名與文件位置
模板文件
先看模板文件(temp2.xlsx),隨筆開頭已經對比過新老模板的區別。這里必須指出:批出中的標簽只能在一行,不能換行!
注意看有兩處批注,其中第一處批注信息為: jx:area(lastCell = "E4")
第二處批注信息為: jx:each(items="reclist" var="receiver" lastCell="E4") —— 請勿將標簽換行!
導出的后端代碼:
@RequestMapping(value = "/export2") public String testExport2() throws Exception{ List<receiver> reclist = reSe.findall(); System.out.println("導出前查詢list:====================="); System.out.println(reclist); InputStream in = receiverController.class.getClassLoader().getResourceAsStream("temp2.xlsx"); OutputStream out = new FileOutputStream("F:/output.xlsx"); Context context = new Context(); context.putVar("reclist",reclist); JxlsHelper.getInstance().processTemplate(in, out, context); SysoUtils.print("導出完畢!"); return null; }
導出內容如下:(實際操作時請規范使用)
//其中,時間格式可以修改模板處單元格格式,詳見jxls1.x隨筆
再修改為導出給瀏覽器下載:
@RequestMapping(value = "/export2") public String testExport2(HttpServletResponse response) throws Exception{ List<receiver> reclist = reSe.findall(); System.out.println("導出前查詢list:====================="); System.out.println(reclist); InputStream in = receiverController.class.getClassLoader().getResourceAsStream("temp2.xlsx"); OutputStream out = null; response.setHeader("Content-Disposition", "attachment;filename=export.xlsx"); /*response.setContentType("application/ms-excel;charset=UTF-8");*/ response.setContentType("application/vnd..ms-excel;charset=UTF-8"); out = response.getOutputStream(); Context context = new Context(); context.putVar("reclist",reclist); JxlsHelper.getInstance().processTemplate(in, out, context); SysoUtils.print("導出完畢!"); return null; }