EasyExcel按照模板填充数据

pom 文件

<dependency>
    <!--导出到excel-->
    <groupId>net.sf.jxls</groupId>
    <artifactId>jxls-core</artifactId>
    <version>1.0.6</version>
</dependency>
<!--4.1.2对应easy-excel版本-->
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>4.1.2</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>4.1.2</version>
</dependency>
<dependency>
	<groupId>com.alibaba</groupId>
	<artifactId>easyexcel</artifactId>
	<version>2.2.10</version>
</dependency>
<!--easy-excel -->

EasyExcel 按照模板填充数据

OutputStream out = null;
BufferedOutputStream bos = null;
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("utf-8");
String fileName = "文件名称.xls";
if (StringUtils.contains(request.getHeader("USER-AGENT"), "Firefox") || request.getHeader("User-Agent").toUpperCase().indexOf("MSIE") > 0) {
    fileName = new String(fileName.getBytes(StandardCharsets.UTF_8), "ISO8859-1");
} else {
    fileName = URLEncoder.encode(fileName, "UTF-8");
}
response.setHeader("Content-disposition", "attachment; filename=" + fileName);
out = response.getOutputStream();
bos = new BufferedOutputStream(out);

ClassPathResource cpr = new ClassPathResource("excel" + File.separator + "模板文件.xls");
ExcelWriter excelWriter = EasyExcel.write(bos)
    .withTemplate(cpr.getInputStream()).excelType(ExcelTypeEnum.XLS).build();
WriteSheet writeSheet = EasyExcel.writerSheet().build();
excelWriter.fill(pageList, writeSheet);

// 别忘记关闭流
excelWriter.finish();
bos.flush();
bos.close();
posted @ 2021-10-09 10:48  紫川先生  阅读(3255)  评论(0编辑  收藏  举报