原因排查
execl文件位置
经过编译后文件大小从10k变到17k
文件打开错误
解决方法
pom中增加如下打包插件,可以过滤掉execl文件不进行编译
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<version>2.6</version>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<encoding>UTF-8</encoding>
<nonFilteredFileExtensions>
<nonFilteredFileExtension>xlsx</nonFilteredFileExtension>
</nonFilteredFileExtensions>
</configuration>
</plugin>
文件下载
@GetMapping("/download/template")
public void downloadTemplate(HttpServletResponse response) throws Exception {
downloadTemplate(response,"car_template.xlsx");
}
/**
* 下载execl模板
*
* @param response
* @param templateName
* @throws Exception
*/
public void downloadTemplate(HttpServletResponse response, String templateName) throws Exception {
try (ServletOutputStream outputStream = response.getOutputStream();
InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream("template/" + templateName)
) {
byte[] bytes = IoUtil.readBytes(resourceAsStream);
//响应客户端浏览器区分数据
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment;filename="+templateName);
response.setCharacterEncoding("utf-8");
IoUtil.write(outputStream, true, bytes);
outputStream.flush();
} catch (Exception e) {
e.printStackTrace();
}
}