dependencia 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>
Código
/**
*
* @param response response
* @param template template name
* @param contextMap context dictionary
* @param downloadFilename display name after download
*/
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);
}
}
.