java 把数据导出成表格下载
现在有很多poi把数据导出的,本篇博客使用的jxls poi 把数据导出到xls 表格中的.
//使用jxls poi需要在pom.xml中加上依赖配置
<dependency>
<groupId>org.jxls</groupId>
<artifactId>jxls-poi</artifactId>
<version>1.0.13</version>
</dependency>
//然后就需要使用jxls进行操作
Context context=new Context();
List<Entity> lawFirmAnnualInspections=new ArrayList<>();
context.putVar("zoneName", "测试的数据");//存入你想在xls模板中表达式展示的值
context.putVar("lawFirmAnnualInspections", "测试的数据");
Map<String,Object> map=new HashMap<>();
//此处的属性对应模板中取值的属性(ps:我就不一一罗列出来了)
map.put("index", i++);
map.put("name", $display(lawyer_annualInspection, "practiceOrg"));
lawFirmAnnualInspections.add(map);
URL url= ResourceUtils.getURL("classpath:/****.xls");//获取xls的url
try(ByteArrayOutputStream byteArrayOutputStream =new ByteArrayOutputStream();){
JxlsHelper.getInstance().processTemplate(url.openStream(),byteArrayOutputStream,context);
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment;filename*=utf-8'zh_cn'" + URLEncoder.encode("下载下来的文件名", "UTF-8"));
response.setHeader("Content-Length", byteArrayOutputStream.size() + "");
response.getOutputStream().write(byteArrayOutputStream.toByteArray());
}
[模板链接地址](https://download.csdn.net/download/qq_42718938/12465610)
/*
这里使用了spring-core-3.1.2.ReLEASE-sources.jar包的方法(ps:以下是getURL()封装的代码)
Thread.currentThread().getContextClassLoader().getResource();
*/