赞
踩
直接用文件流导出
public void getTemplate(HttpServletRequest request, HttpServletResponse response) { try (InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("template/template.xls"); OutputStream out = response.getOutputStream() ){ if (inputStream == null) { throw new GlobalException("模板不存在!"); } response.setContentType("application/octet-stream"); String fileName = URLEncoder.encode("模板","utf-8"); response.setCharacterEncoding("utf-8"); response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xls"); response.setContentType("application/vnd.ms-excel ;charset=UTF-8"); byte[] bytes = new byte[1024 * 2]; while(inputStream.read(bytes) != -1){ out.write(bytes); } out.flush(); } catch (Exception e) { e.printStackTrace(); throw new GlobalException("导出失败!"); } }
结果会文件损坏,或乱码,如下图
pom文件的build下添加关闭压缩配置
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<version>2.6</version>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<encoding>UTF-8</encoding>
<nonFilteredFileExtensions>
<nonFilteredFileExtension>xls</nonFilteredFileExtension>
<nonFilteredFileExtension>xlsx</nonFilteredFileExtension>
</nonFilteredFileExtensions>
</configuration>
</plugin>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。