java实现excel模板导出数据

web项目导出excel有很多种方法,个人觉得使用excel模板导出比较好用,可以满足甲方对excel格式的多种需求,而且实现起来方便。

准备需要的jar:下载地址
freemarker-2.3.19.jar
freemarker-util-0.0.1.jar
jxl-2.6.10.jar
jxl-report-1.0.jar

maven项目pom.xml配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<!-- excel模板依赖start -->
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.19</version>
</dependency>
<dependency>
<groupId>net.sourceforge.jexcelapi</groupId>
<artifactId>jxl</artifactId>
<version>2.6.10</version>
</dependency>
<dependency>
<groupId>fakepath</groupId>
<artifactId>freemarker-util</artifactId>
<version>0.0.1</version>
</dependency>
<dependency>
<groupId>fakepath</groupId>
<artifactId>jxl-report</artifactId>
<version>1.0</version>
</dependency>
<!-- excel模板依赖 end-->

java代码实现:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
@RequestMapping("/exportExcel")
@ResponseBody
public Map<String,Object> exportExcel(HttpServletRequest request,@RequestBody Map<String, Object> map) throws Exception {
logger.info("------------开始执行下载任务-----------");
Map<String,Object> result = new HashMap<String,Object>();
result.put("result",true);
result.put("msg","执行成功");
try {
String downloadPath="/export";//导出文件夹
//查询导出数据
Map<String,Object> resultMap = reportService.queryExportData(map);
//目录生成
ExcelUtil.mkdir(downloadPath);
String filename = UUID.randomUUID().toString().replace("-", "").toUpperCase()+".xls";
File f = new File(downloadPath+"/" + File.separatorChar + filename);
// 模板生成Excel
ReportEnginer enginer = new ReportEnginer();
//模板存储路径
String modelPath = request.getSession().getServletContext().getRealPath("/")+ "/template/model.xls";
InputStream inputStream = new FileInputStream(new File(modelPath));
OutputStream outputStream = new FileOutputStream(f);
enginer.excute(inputStream, resultMap, outputStream);
inputStream.close();
outputStream.close();
downloadDetail.setResult(filename);

} catch (Exception e) {
e.printStackTrace();
result.put("result",false);
result.put("msg","执行失败");
logger.info("------------下载任务执行失败-----------");
}
logger.info("------------下载任务执行完成-----------");
return result;
}

代码中resultMap如下:
{datalist=[{hours=9, name=张三, cost=10}, {hours=32, name=李四, cost=6}]}
excle模板:使用etl表达式

结果:

本文结束啦感谢您的阅读

本文标题:java实现excel模板导出数据

文章作者:Smartfoot

原始链接:http://blog.bestsmartfoot.top/2018/07/06/article-4/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

公众号