经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » Java相关 » Spring » 查看文章
SpringBoot向Excel模板中写入数据并下载 (无需获取file对象及模板绝对路径)
来源:cnblogs  作者:努力的小韩  时间:2022/12/12 9:14:25  对本文有异议

之前用获取模板路径的方式测试没问题打包后就有问题了
image
莫名出现一个! 找了很多教程尝试无果 最终使用下面这个方式
无需获取file对象以及模板路径的方式进行写入下载
(那个设置浏览器编码没有测试不知道能不能用!!!)

  1. public void export(SampleFilterAO filter, HttpServletResponse response, HttpServletRequest request) {
  2. Map<String, Object> map = new HashMap<>();
  3. // 获取导出的时间参数
  4. String date = request.getParameter("Date");
  5. map.put("Date", date);
  6. String fileName = "data.xlsx";
  7. // 使用类加载器获取excel文件流,基于模板填充数据
  8. ClassPathResource classPathResource = new ClassPathResource(fileName);
  9. InputStream is = null;
  10. XSSFWorkbook workbook = null;
  11. try {
  12. is = classPathResource.getInputStream();
  13. workbook = new XSSFWorkbook(is);
  14. XSSFSheet sheet = null;
  15. // 获取第一个sheet页
  16. // getSheet的参数是sheet的名称, 获取具体名称的sheet。
  17. sheet = workbook.getSheetAt(0);
  18. Long offset = (filter.getPage() - 1) * filter.getLimit();
  19. filter.setOffset(offset);
  20. filter.setLimit(sampleMapper.count(filter).intValue());
  21. List<Sample> resutList = sampleMapper.list(filter);
  22. for (int i = 0; i < resutList.size(); i++) {
  23. Integer j=i+1;
  24. writeExcel(sheet, resutList, j, i);
  25. }
  26. } catch (IOException e) {
  27. e.printStackTrace();
  28. }
  29. //文件下載
  30. response.reset();
  31. response.setContentType("text/html;charset=UTF-8");
  32. response.setContentType("application/x-msdownload");
  33. String newName = "";
  34. try {
  35. newName = URLEncoder.encode("扫描记录导出" + System.currentTimeMillis() + ".xlsx", "UTF-8");
  36. String s = encodeFileName(request,newName);
  37. response.addHeader("Content-Disposition", "attachment;filename=\"" + s + "\"");
  38. OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
  39. workbook.write(toClient);
  40. toClient.flush();
  41. } catch (UnsupportedEncodingException e) {
  42. // TODO Auto-generated catch block
  43. e.printStackTrace();
  44. } catch (IOException e) {
  45. // TODO Auto-generated catch block
  46. e.printStackTrace();
  47. }
  48. }
  49. //写入数据的方法
  50. public void writeExcel(XSSFSheet sheet, List<Sample> resutList,Integer rownum, Integer index) {
  51. if (resutList.get(index) != null && !"".equals(resutList.get(index))) {
  52. Row row = sheet.createRow(rownum);
  53. Cell cell = row.createCell(0); //序号
  54. cell.setCellValue(index+1);
  55. String sampleNo = resutList.get(index).getSampleNo();// 玻片编号
  56. cell = row.createCell(1);
  57. cell.setCellValue(sampleNo);
  58. String patientNo = resutList.get(index).getPatientNo();// 病案号
  59. cell = row.createCell(2);
  60. cell.setCellValue(patientNo);
  61. String patientName = resutList.get(index).getPatientName();// 姓名
  62. cell = row.createCell(3);
  63. cell.setCellValue(patientName);
  64. String patientSex = resutList.get(index).getPatientSex();// 性别
  65. cell = row.createCell(4);
  66. cell.setCellValue(patientSex);
  67. String position = resutList.get(index).getPosition();// 部位
  68. cell = row.createCell(5);
  69. cell.setCellValue(position);
  70. String aiResultId = resutList.get(index).getAiResultId();// AI检验结果
  71. cell = row.createCell(6);
  72. cell.setCellValue(aiResultId);
  73. String createdName = resutList.get(index).getCreatedName();// 扫描人员
  74. cell = row.createCell(7);
  75. cell.setCellValue(createdName);
  76. Long createdAt = resutList.get(index).getCreatedAt();// 记录时间
  77. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  78. String time = format.format(createdAt);
  79. cell = row.createCell(8);
  80. cell.setCellValue(time);
  81. }
  82. }
  83. //不同浏览器设置不同编码(未测试!)
  84. public static String encodeFileName(HttpServletRequest request, String fileName)
  85. throws UnsupportedEncodingException {
  86. String newFilename = URLEncoder.encode(fileName, "UTF8").replaceAll("\\+", "%20");
  87. String agent = request.getHeader("USER-AGENT").toLowerCase();
  88. if (null != agent && -1 != agent.indexOf("msie")) {
  89. /**
  90. * IE浏览器,只能采用URLEncoder编码
  91. */
  92. return newFilename;
  93. } else if (null != agent && -1 != agent.indexOf("applewebkit")) {
  94. /**
  95. * Chrome浏览器,只能采用ISO编码的中文输出
  96. */
  97. return new String(fileName.getBytes("UTF-8"), "ISO8859-1");
  98. } else if (null != agent && -1 != agent.indexOf("opera")) {
  99. /**
  100. * Opera浏览器只可以使用filename*的中文输出
  101. * RFC2231规定的标准
  102. */
  103. return newFilename;
  104. } else if (null != agent && -1 != agent.indexOf("safari")) {
  105. /**
  106. * Safani浏览器,只能采用iso编码的中文输出
  107. */
  108. return new String(fileName.getBytes("UTF-8"), "ISO8859-1");
  109. } else if (null != agent && -1 != agent.indexOf("firefox")) {
  110. /**
  111. * Firfox浏览器,可以使用filename*的中文输出
  112. * RFC2231规定的标准
  113. */
  114. return newFilename;
  115. } else {
  116. return newFilename;
  117. }
  118. }

本文转自:https://blog.csdn.net/wongrock/article/details/118359816

原文链接:https://www.cnblogs.com/HanYork/p/16971314.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号