maven依赖
<dependency>
<groupId>org.jxls</groupId>
<artifactId>jxls</artifactId>
</dependency>
<dependency>
<groupId>org.jxls</groupId>
<artifactId>jxls-poi</artifactId>
</dependency>
<dependency>
<groupId>org.jxls</groupId>
<artifactId>jxls-jexcel</artifactId>
</dependency>
<dependency>
<groupId>org.jxls</groupId>
<artifactId>jxls-reader</artifactId>
</dependency>
<dependency>
<groupId>net.sourceforge.jexcelapi</groupId>
<artifactId>jxl</artifactId>
<version>2.6.12</version>
</dependency>
代码实现
/**
*
* @param response response
* @param template 模板名
* @param contextMap context字典
* @param downloadFilename 下载后展示名
*/
public void doDownloadExcel(HttpServletResponse response,
String template,
Map<String, Object> contextMap,
String downloadFilename) {
try (InputStream inputStream = getClass().getClassLoader().getResourceAsStream("static/excel/" + template)) {
response.setContentType("application/octet-stream;charset=UTF-8");
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-Disposition", "attachment;filename=" +
new String(downloadFilename.getBytes(), StandardCharsets.ISO_8859_1) + ".xls");
OutputStream outputStream = response.getOutputStream();
Context context = new Context();
contextMap.forEach(context::putVar);
JxlsHelper.getInstance().processTemplate(inputStream, outputStream, context);
} catch (Exception e) {
log.error("[download] error ", e);
}
}