ReportDownloadController.java 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package com.persagy.proxy.adm.controller;
  2. import java.io.File;
  3. import java.io.FileNotFoundException;
  4. import java.net.URLEncoder;
  5. import java.nio.charset.StandardCharsets;
  6. import javax.servlet.http.HttpServletRequest;
  7. import javax.servlet.http.HttpServletResponse;
  8. import com.alibaba.excel.ExcelWriter;
  9. import org.springframework.util.ResourceUtils;
  10. import org.springframework.web.bind.annotation.RequestMapping;
  11. import org.springframework.web.bind.annotation.RequestParam;
  12. import org.springframework.web.bind.annotation.RestController;
  13. import com.alibaba.excel.EasyExcel;
  14. import com.persagy.proxy.adm.constant.AdmCommonConstant;
  15. import lombok.extern.slf4j.Slf4j;
  16. /**
  17. * 报表/模板下载
  18. *
  19. * @version 1.0.0
  20. * @company persagy
  21. * @author zhangqiankun
  22. * @date 2021年8月30日 下午7:41:56
  23. */
  24. @Slf4j
  25. @RestController
  26. public class ReportDownloadController {
  27. /**
  28. * 下载报告-设备静态信息维护模板
  29. *
  30. * @param request
  31. * @param response
  32. */
  33. @RequestMapping("/graphic/downloads/equip/template")
  34. public void downloadEquipTemplate(HttpServletRequest request, HttpServletResponse response) {
  35. ExcelWriter excelWriter = null;
  36. try {
  37. response.setContentType("application/vnd.ms-excel");
  38. String encode = StandardCharsets.UTF_8.name();
  39. response.setCharacterEncoding(encode);
  40. // 防止中文乱码
  41. String fileName = URLEncoder.encode("台账信息导入模板", encode);
  42. response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
  43. File file = null;
  44. try {
  45. file = ResourceUtils.getFile(ResourceUtils.CLASSPATH_URL_PREFIX + "template/equip_template.xlsx");
  46. } catch (FileNotFoundException e) {
  47. }
  48. if (file == null || !file.exists()) {
  49. file = ResourceUtils.getFile(AdmCommonConstant.SERVER_ROOT_PATH + "/template/equip_template.xlsx");
  50. }
  51. excelWriter = EasyExcel.write(response.getOutputStream()).withTemplate(file).build();
  52. } catch (Exception e) {
  53. log.error("设备静态信息维护模板下载失败", e);
  54. } finally {
  55. if (excelWriter != null) {
  56. excelWriter.finish();
  57. }
  58. }
  59. }
  60. /**
  61. * 下载报告-点表
  62. *
  63. * @param request
  64. * @param response
  65. */
  66. @RequestMapping("/graphic/downloads/point")
  67. public void downloadPoint(@RequestParam(required = false) String key, HttpServletRequest request, HttpServletResponse response) {
  68. ExcelWriter excelWriter = null;
  69. try {
  70. response.setContentType("application/vnd.ms-excel");
  71. String encode = StandardCharsets.UTF_8.name();
  72. response.setCharacterEncoding(encode);
  73. response.setHeader("Content-disposition", "attachment;filename=" + key);
  74. File file = null;
  75. try {
  76. file = ResourceUtils.getFile(ResourceUtils.CLASSPATH_URL_PREFIX + "template/upload/" + key);
  77. } catch (FileNotFoundException e) {
  78. file = ResourceUtils.getFile(ResourceUtils.CLASSPATH_URL_PREFIX + "template/relation-template.xlsx");
  79. }
  80. if (file == null || !file.exists()) {
  81. file = ResourceUtils.getFile(AdmCommonConstant.SERVER_ROOT_PATH + "/template/relation-template.xlsx");
  82. }
  83. excelWriter = EasyExcel.write(response.getOutputStream()).withTemplate(file).build();
  84. } catch (Exception e) {
  85. log.error("设备静态信息维护模板下载失败", e);
  86. } finally {
  87. if (excelWriter != null) {
  88. excelWriter.finish();
  89. }
  90. }
  91. }
  92. }