ExcelResource.java 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. package com.common.workflow.web.rest;
  2. import com.common.workflow.service.util.FileUtils;
  3. import com.common.workflow.web.rest.vm.*;
  4. import com.github.crab2died.ExcelUtils;
  5. import org.slf4j.Logger;
  6. import org.slf4j.LoggerFactory;
  7. import org.springframework.web.bind.annotation.PostMapping;
  8. import org.springframework.web.bind.annotation.RequestBody;
  9. import org.springframework.web.bind.annotation.RequestMapping;
  10. import org.springframework.web.bind.annotation.RestController;
  11. import javax.servlet.http.HttpServletResponse;
  12. import javax.validation.Valid;
  13. import java.io.File;
  14. import java.io.OutputStream;
  15. import java.text.SimpleDateFormat;
  16. import java.util.*;
  17. /**
  18. * Created by txz on 2020-10-15.
  19. */
  20. @RestController
  21. @RequestMapping("/api/excel")
  22. public class ExcelResource {
  23. private final Logger log = LoggerFactory.getLogger(ExcelResource.class);
  24. public ExcelResource() { }
  25. @PostMapping("/fill-excel")
  26. public void fillExcelTemplate(@Valid @RequestBody ExcelTemplateVM wordTemplateVM, HttpServletResponse response) throws Exception {
  27. response.reset();
  28. response.setContentType("multipart/form-data;charset=utf-8");
  29. response.setHeader("content-disposition", "attachment;filename=" + wordTemplateVM.getFileName());
  30. response.setHeader("Pragma","No-cache");
  31. response.setHeader ( "Cache-Control", "no-store");
  32. response.setHeader("Access-Control-Allow-Origin", "*");
  33. response.setHeader("Access-Control-Allow-Headers", "*");
  34. OutputStream outputStream = response.getOutputStream();
  35. String fullFileName = FileUtils.getExcelTemplatePath() + "/" + wordTemplateVM.getFileName();
  36. File file = new File(fullFileName);
  37. if (file.exists()) {
  38. file.delete(); //清除历史文件
  39. }
  40. fullFileName = FileUtils.downLoadFromUrl(wordTemplateVM.getTemplateUrl(), wordTemplateVM.getFileName(), FileUtils.getExcelTemplatePath());
  41. if(wordTemplateVM.getContractClass().equals("03")){
  42. List<OilContractSumScoreVo> reportList = new ArrayList<>();
  43. ArrayList<LinkedHashMap<String,Object>> list = (ArrayList<LinkedHashMap<String,Object>>) wordTemplateVM.getDatas().get("data");
  44. for(LinkedHashMap<String,Object> entry : list){
  45. reportList.add(new OilContractSumScoreVo(entry.get("SupplierName").toString(),
  46. entry.get("SumScore").toString(),entry.get("SumScore1").toString(),entry.get("SumScore2").toString(),
  47. entry.get("SumScore3").toString(), entry.get("SumScore4").toString(),
  48. entry.get("Conclusion").toString().equals("1") ? "是" : "",
  49. entry.get("Conclusion").toString().equals("2") ? "是" : "",
  50. entry.get("Conclusion").toString().equals("3") ? "是" : ""));
  51. }
  52. Map<String, String> data = new HashMap<>();
  53. data.put("title", "大港油田公司服务商考核评价表");
  54. Date d = new Date();
  55. SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
  56. data.put("info", sdf.format(d));
  57. ExcelUtils.getInstance().exportObjects2Excel(fullFileName, 0, reportList, data, OilContractSumScoreVo.class, false, outputStream);
  58. }else{
  59. List<OilContractSumScoreVo2> reportList = new ArrayList<>();
  60. ArrayList<LinkedHashMap<String,Object>> list = (ArrayList<LinkedHashMap<String,Object>>) wordTemplateVM.getDatas().get("data");
  61. for(LinkedHashMap<String,Object> entry : list){
  62. reportList.add(new OilContractSumScoreVo2(entry.get("SupplierName").toString(),
  63. entry.get("SumScore").toString(),entry.get("SumScore1").toString(),entry.get("SumScore2").toString(),
  64. entry.get("SumScore3").toString(), entry.get("SumScore4").toString(), entry.get("SumScore5").toString(),
  65. entry.get("Conclusion").toString().equals("1") ? "是" : "",
  66. entry.get("Conclusion").toString().equals("2") ? "是" : "",
  67. entry.get("Conclusion").toString().equals("3") ? "是" : ""));
  68. }
  69. Map<String, String> data = new HashMap<>();
  70. data.put("title", "大港油田公司物资供应商考核评价表(全部、一级、二级)");
  71. Date d = new Date();
  72. SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
  73. data.put("info", sdf.format(d));
  74. ExcelUtils.getInstance().exportObjects2Excel(fullFileName, 0, reportList, data, OilContractSumScoreVo2.class, false, outputStream);
  75. }
  76. outputStream.flush();
  77. outputStream.close();
  78. }
  79. @PostMapping("/contrast-excel")
  80. public void ContrastExcelTemplate(@Valid @RequestBody ExcelTemplateVM wordTemplateVM, HttpServletResponse response) throws Exception {
  81. response.reset();
  82. response.setContentType("multipart/form-data;charset=utf-8");
  83. response.setHeader("content-disposition", "attachment;filename=" + wordTemplateVM.getFileName());
  84. response.setHeader("Pragma","No-cache");
  85. response.setHeader ( "Cache-Control", "no-store");
  86. response.setHeader("Access-Control-Allow-Origin", "*");
  87. response.setHeader("Access-Control-Allow-Headers", "*");
  88. OutputStream outputStream = response.getOutputStream();
  89. String fullFileName = FileUtils.getExcelTemplatePath() + "/" + wordTemplateVM.getFileName();
  90. File file = new File(fullFileName);
  91. if (file.exists()) {
  92. file.delete(); //清除历史文件
  93. }
  94. fullFileName = FileUtils.downLoadFromUrl(wordTemplateVM.getTemplateUrl(), wordTemplateVM.getFileName(), FileUtils.getExcelTemplatePath());
  95. Map<String, List<?>> classes = new HashMap<>();
  96. List<ContrastExcelVM> reportList = new ArrayList<>();
  97. List<ContrastExcelVM> reportList1 = new ArrayList<>();
  98. List<ContrastExcelVM> reportList2 = new ArrayList<>();
  99. ArrayList<LinkedHashMap<String,Object>> list = (ArrayList<LinkedHashMap<String,Object>>) wordTemplateVM.getDatas().get("data");
  100. for(LinkedHashMap<String,Object> entry : list){
  101. ContrastExcelVM contrastExcelVM = new ContrastExcelVM();
  102. contrastExcelVM.setSupplierName(entry.get("SupplierName").toString());
  103. contrastExcelVM.setInFlag(entry.get("InFlag").toString().equals("1") ? "准入" : entry.get("InFlag").toString().equals("2") ? "暂停" : "取消");
  104. contrastExcelVM.setAccessCardNo(entry.get("AccessCardNo").toString());
  105. contrastExcelVM.setSupplierTypeCode(entry.get("SupplierTypeCode").toString().equals("01") ? "物资类" : entry.get("SupplierTypeCode").toString().equals("02") ? "基建类" : "服务类");
  106. contrastExcelVM.setLegalPerson(entry.get("LegalPerson").toString());
  107. contrastExcelVM.setRegCapital(entry.get("RegCapital").toString());
  108. contrastExcelVM.setMobile(entry.get("Mobile").toString());
  109. contrastExcelVM.setSubCnt(entry.get("SubCnt").toString());
  110. contrastExcelVM.setNoSubCnt(entry.get("NoSubCnt").toString());
  111. contrastExcelVM.setOneTwoCount(entry.get("OneTwoCount").toString());
  112. contrastExcelVM.setTwoOneCount(entry.get("TwoOneCount").toString());
  113. contrastExcelVM.setChecked(entry.get("Checked").toString().equals("1") ? "否" : "是");
  114. if (entry.get("MinClassId").toString().equals("0")) {
  115. reportList2.add(contrastExcelVM);
  116. } else {
  117. if (entry.get("Checked").toString().equals("1")) {
  118. reportList.add(contrastExcelVM);
  119. } else {
  120. reportList1.add(contrastExcelVM);
  121. }
  122. }
  123. }
  124. classes.put("class_two", reportList);
  125. classes.put("class_one", reportList1);
  126. classes.put("class_three", reportList2);
  127. Map<String, String> data = new HashMap<>();
  128. data.put("title", "资质导入对比表");
  129. Date d = new Date();
  130. SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
  131. data.put("info", sdf.format(d));
  132. ExcelUtils.getInstance().exportObject2Excel(fullFileName, 0, classes, data, ContrastExcelVM.class, false, outputStream);
  133. outputStream.flush();
  134. outputStream.close();
  135. }
  136. }