說明:本文是jxls根據(jù)模板導(dǎo)出Excel直接下載。
使用版本:jxls V2.10.0
excel模板版本:.xlsx格式
jxls官網(wǎng)地址:https://jxls.sourceforge.net/index.html
1、pom引用
<dependency> <groupId>org.jxls</groupId> <artifactId>jxls</artifactId> <version>2.10.0</version> </dependency> <dependency> <groupId>org.jxls</groupId> <artifactId>jxls-poi</artifactId> <version>2.10.0</version> </dependency> <dependency> <groupId>org.jxls</groupId> <artifactId>jxls-jexcel</artifactId> <version>1.0.8</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-jexl</artifactId> <version>2.1.1</version> </dependency>
2、Controller調(diào)用方法
@PostMapping("/export") public AjaxResult export(HttpServletResponse response, Data data) throws IOException { String fileName = "test.xlsx"; Map<String, Object> model = dataService.getExportData(data,fileName);//拼接需要導(dǎo)出的內(nèi)容 response.addHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\""); return JxlsUtils.exportExcelWithOS(response.getOutputStream(),fileName, model); }
3、拼接數(shù)據(jù)
public Map<String, Object> getExportData(Data data,String fileName) { Map<String, Object> model = new HashMap<String, Object>(); // 綁定數(shù)據(jù) try { //可綁定單個參數(shù),Excel模板中單元格直接用${test1}獲取 model.put("test1", "ttt"); //可綁定list,使用${item.參數(shù)名}獲取,需在列表首個單元格中增加批注 List<Data1> taskItems = new ArrayList<Data1>();//Data1為某個數(shù)據(jù)類 for (int i=0;i<5;i++) { Data1 d=new Data1; //d.setA("111"); taskItems.add(d); } model.put("taskItems", taskItems); }catch (Exception ex){ logger.error(ex.getMessage()); System.out.println(ex.getMessage()); } return model; }
4、Excel導(dǎo)出方法
import org.apache.commons.jexl3.JexlBuilder; import org.apache.commons.jexl3.JexlEngine; import org.jxls.builder.xls.XlsCommentAreaBuilder; import org.jxls.common.Context; import org.jxls.expression.JexlExpressionEvaluator; import org.jxls.transform.Transformer; import org.jxls.transform.poi.PoiTransformer; import org.jxls.util.JxlsHelper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.*; import java.util.HashMap; import java.util.Map; public class JxlsUtils { static{ } /** * 根據(jù)模板生成文件,直接下載 * @param os 流數(shù)據(jù) * @param templateFileName 模板文件名稱 * @param model 填充數(shù)據(jù) * @throws IOException */ public static AjaxResult exportExcelWithOS(OutputStream os , String templateFileName, Map<String, Object> model) throws IOException{ // 獲取模板文件 InputStream is = new FileInputStream(new File(模板文件路徑地址 + templateFileName)); try { // 輸出 Context context = PoiTransformer.createInitialContext(); if (model != null) { for (String key : model.keySet()) { context.putVar(key, model.get(key)); } } JxlsHelper.getInstance().setUseFastFormulaProcessor(false).setEvaluateFormulas(true).processTemplate(is, os, context); } catch (Exception ex){ System.out.println(ex.getMessage()); logger.error(ex.getMessage()); return AjaxResult.error(ex.getMessage()); }finally { is.close(); } return AjaxResult.success(); } }
5、Excel模板,創(chuàng)建xlsx格式模板
添加批注,第一個單元格添加范圍批注,jx:area(lastCell="K3")
list列表增加jx:each(items="taskItems" var="item" lastCell="K3")
?文章來源地址http://www.zghlxwxcb.cn/news/detail-492159.html
之前寫過另一種方法,先根據(jù)模板生成Excel,然后調(diào)用方法導(dǎo)出,可參考:https://www.cnblogs.com/webttt/p/14283481.html文章來源:http://www.zghlxwxcb.cn/news/detail-492159.html
?
到了這里,關(guān)于jxls根據(jù)模板導(dǎo)出Excel(二)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!