当前位置:   article > 正文

根据excel模板导出excel表格 返回流给前端_后端生成excel文件,以流的形式返回给前端

后端生成excel文件,以流的形式返回给前端

java代码

pom文件
  1. <dependency>
  2. <groupId>com.alibaba</groupId>
  3. <artifactId>easyexcel</artifactId>
  4. <version>2.2.4</version>
  5. </dependency>
java代码
  1. public void exportStoreSignList(HttpServletResponse response,@RequestParam(value="createTime",required = false)String createTime,@RequestParam(value="topic",required = false)String topic,
  2. @RequestParam(value="nowTime",required = false)String nowTime,@RequestParam(value="name",required = false)String name,
  3. @RequestParam(value="tableName",required = false)String tableName) throws Exception {
  4. //获取模板
  5. ClassPathResource classPathResource = new ClassPathResource("/excel/excelCharts.xlsx");
  6. //输入流
  7. InputStream inputStream = null;
  8. //输出流
  9. ServletOutputStream outputStream = null;
  10. //处理填充数据
  11. List<Map<String,Object>> storeList = new ArrayList<>();
  12. storeList = this.excelcharts(createTime,topic);
  13. ExcelWriter excelWriter = null;
  14. Map<String,Object> map = new HashMap<>();
  15. if(tableName != null && tableName != ""){
  16. map.put("name",name);
  17. }else{
  18. map.put("name","");
  19. }
  20. if(tableName != null && tableName != ""){
  21. String rkDateFormat = createTime.substring(0, 4) + "年" + createTime.substring(5, 7) + "月" + createTime.substring(8, 10) + "日";
  22. map.put("createTime",rkDateFormat);
  23. }else{
  24. map.put("createTime","");
  25. }
  26. try {
  27. //输入流
  28. inputStream = classPathResource.getInputStream();
  29. String fileName = URLEncoder.encode(tableName, "UTF-8");
  30. response.setCharacterEncoding("utf-8");
  31. //返回的数据类型
  32. response.setContentType("application/vnd.ms-excel;charset=utf-8");
  33. //响应头
  34. response.setHeader("Content-Disposition", "attachment;fileName="
  35. + fileName);
  36. outputStream = response.getOutputStream();
  37. excelWriter = EasyExcel.write(outputStream).withTemplate(inputStream).build();
  38. WriteSheet writeSheet = EasyExcel.writerSheet().build();
  39. FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.TRUE).build();
  40. excelWriter.fill(map, writeSheet);
  41. excelWriter.fill(new FillWrapper("data", storeList), fillConfig, writeSheet);
  42. outputStream.flush();
  43. excelWriter.finish();
  44. } catch (IOException e) {
  45. e.printStackTrace();
  46. throw new RuntimeException("导出错误!");
  47. } catch (Exception e) {
  48. e.printStackTrace();
  49. throw new RuntimeException("异常!");
  50. }finally {
  51. // 千万别忘记finish 会帮忙关闭流
  52. if (excelWriter != null) {
  53. excelWriter.finish();
  54. }
  55. //关闭流
  56. if (outputStream != null) {
  57. try {
  58. outputStream.close();
  59. } catch (IOException e) {
  60. e.printStackTrace();
  61. }
  62. }
  63. if (inputStream != null) {
  64. try {
  65. inputStream.close();
  66. } catch (IOException e) {
  67. e.printStackTrace();
  68. }
  69. }
  70. }
  71. }

vue

  1. await exportStoreList(createTime,topic,nowTime,name,tableName).then((res) => {
  2. console.log("exportStoreList==================>",res)
  3. if (!res) {
  4. return
  5. }
  6. let url = window.URL.createObjectURL(new Blob([res]))
  7. let link = document.createElement('a')
  8. link.style.display = 'none'
  9. link.href = url
  10. link.setAttribute('download', this.excelChartsName)
  11. document.body.appendChild(link)
  12. link.click()
  13. }).catch(err => console.log('下载失败', err))
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/黑客灵魂/article/detail/842540
推荐阅读
相关标签
  

闽ICP备14008679号