fa0be0d5080cb6ee2a72166ebae719eb5c1732a0.svn-base 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. package cn.com.goldenwater.dcproj.service.impl.ducha;
  2. import cn.com.goldenwater.core.service.AbstractCrudService;
  3. import cn.com.goldenwater.dcproj.dao.BisInspPlanChkDao;
  4. import cn.com.goldenwater.dcproj.model.BisInspPlanChk;
  5. import cn.com.goldenwater.dcproj.param.BisInspPlanChkParam;
  6. import cn.com.goldenwater.dcproj.service.BisInspPlanChkService;
  7. import cn.com.goldenwater.id.util.UuidUtil;
  8. import org.apache.commons.lang3.StringUtils;
  9. import org.apache.poi.ss.usermodel.Cell;
  10. import org.apache.poi.ss.usermodel.Row;
  11. import org.apache.poi.xssf.usermodel.XSSFCell;
  12. import org.apache.poi.xssf.usermodel.XSSFCellStyle;
  13. import org.apache.poi.xssf.usermodel.XSSFSheet;
  14. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  15. import org.springframework.beans.factory.annotation.Autowired;
  16. import org.springframework.beans.factory.annotation.Value;
  17. import org.springframework.stereotype.Service;
  18. import org.springframework.transaction.annotation.Transactional;
  19. import javax.servlet.http.HttpServletResponse;
  20. import java.io.*;
  21. import java.net.URLEncoder;
  22. import java.util.Date;
  23. import java.util.List;
  24. /**
  25. * @author lhc
  26. * @date 2021-4-1
  27. */
  28. @Service
  29. @Transactional
  30. public class BisInspPlanChkServiceImpl extends AbstractCrudService<BisInspPlanChk, BisInspPlanChkParam> implements BisInspPlanChkService {
  31. @Value("${export.templatePath}")
  32. private String templatePath;
  33. @Autowired
  34. private BisInspPlanChkDao bisInspPlanChkDao;
  35. public BisInspPlanChkServiceImpl(BisInspPlanChkDao bisInspPlanChkDao) {
  36. super(bisInspPlanChkDao);
  37. this.bisInspPlanChkDao = bisInspPlanChkDao;
  38. }
  39. @Override
  40. public int insert(BisInspPlanChk bisInspPlanChk) {
  41. String uuid = UuidUtil.uuid(); // 生成uuid
  42. bisInspPlanChk.setId(uuid);
  43. bisInspPlanChk.setIntm(new Date());
  44. bisInspPlanChk.setUptm(new Date());
  45. bisInspPlanChk.setDataStat("0");
  46. return this.bisInspPlanChkDao.insert(bisInspPlanChk);
  47. }
  48. @Override
  49. public int update(BisInspPlanChk bisInspPlanChk) {
  50. bisInspPlanChk.setUptm(new Date());
  51. return this.bisInspPlanChkDao.update(bisInspPlanChk);
  52. }
  53. @Override
  54. public int delete(String id) {
  55. return this.bisInspPlanChkDao.delete(id);
  56. }
  57. @Override
  58. public void exportCheckList(HttpServletResponse response, BisInspPlanChkParam bisInspPlanChkParam) {
  59. String templateFilePath = templatePath + File.separator + "jdjcjh2.xlsx";
  60. if (StringUtils.isNotBlank(bisInspPlanChkParam.getChkDep())) {
  61. templateFilePath = templatePath + File.separator + "jdjcjh1.xlsx";
  62. }
  63. File fi = new File(templateFilePath);
  64. try (InputStream fs = new FileInputStream(fi);
  65. XSSFWorkbook wb = new XSSFWorkbook(fs)) {
  66. // 2.数据赋值
  67. updateCheckList(wb, bisInspPlanChkParam);
  68. // 3.导出xlsx
  69. String title = bisInspPlanChkParam.getChkYear() + "年度水利强监管工作计划表.xlsx";
  70. if (StringUtils.isNotBlank(bisInspPlanChkParam.getChkDep())) {
  71. title = bisInspPlanChkParam.getChkDep() + bisInspPlanChkParam.getChkYear() + "年度水利强监管工作计划表.xlsx";
  72. }
  73. writeToResponse(response, wb, title);
  74. } catch (IOException e) {
  75. e.printStackTrace();
  76. }
  77. }
  78. private void updateCheckList(XSSFWorkbook wb, BisInspPlanChkParam bisInspPlanChkParam) {
  79. // 1.获取数据
  80. List<BisInspPlanChk> checkListData = bisInspPlanChkDao.findList(bisInspPlanChkParam);
  81. // 读取了模板内所有sheet内容
  82. XSSFSheet sheet = wb.getSheetAt(0);
  83. String title = checkListData.get(0).getChkYear() + "年度水利强监管工作计划表";
  84. if (StringUtils.isNotBlank(bisInspPlanChkParam.getChkDep())) {
  85. title = bisInspPlanChkParam.getChkDep() + checkListData.get(0).getChkYear() + "年度水利强监管工作计划表";
  86. }
  87. // 在相应的单元格进行赋值
  88. setCellValue(sheet, 1, 0, title);
  89. // 第 3 行开始录入问题
  90. XSSFCellStyle cellStyle = sheet.getRow(3).getCell(0).getCellStyle();
  91. for (int i = 0; i < checkListData.size(); i++) {
  92. // if (i > 0) {
  93. // 如果不是第一行,创建行
  94. Row row = sheet.createRow(4 + i);
  95. int lie = 9;
  96. if (StringUtils.isNotBlank(bisInspPlanChkParam.getChkDep())) {
  97. lie = 11;
  98. }
  99. for (int j = 0; j < lie; j++) {
  100. Cell cell = row.createCell(j);
  101. cell.setCellStyle(cellStyle);
  102. }
  103. // }
  104. }
  105. for (int i = 0; i < checkListData.size(); i++) {
  106. BisInspPlanChk bisInspPlanChk = checkListData.get(i);
  107. if (StringUtils.isNotBlank(bisInspPlanChkParam.getChkDep())) {
  108. setDepCell(sheet, i, bisInspPlanChk);
  109. } else {
  110. setAllCell(sheet, i, bisInspPlanChk);
  111. }
  112. }
  113. }
  114. private void setAllCell(XSSFSheet sheet, int i, BisInspPlanChk bisInspPlanChk) {
  115. setCellValue(sheet, 4 + i, 0, String.valueOf(i + 1));
  116. setCellValue(sheet, 4 + i, 1, bisInspPlanChk.getChkMatter());
  117. setCellValue(sheet, 4 + i, 2, bisInspPlanChk.getChkCity());
  118. setCellValue(sheet, 4 + i, 3, bisInspPlanChk.getChkCounty());
  119. setCellValue(sheet, 4 + i, 4, bisInspPlanChk.getChkContent());
  120. setCellValue(sheet, 4 + i, 5, bisInspPlanChk.getChkDep());
  121. setCellValue(sheet, 4 + i, 6, bisInspPlanChk.getChkType());
  122. setCellValue(sheet, 4 + i, 7, bisInspPlanChk.getChkTm());
  123. setCellValue(sheet, 4 + i, 8, "");
  124. }
  125. private void setDepCell(XSSFSheet sheet, int i, BisInspPlanChk bisInspPlanChk) {
  126. setCellValue(sheet, 4 + i, 0, String.valueOf(i + 1));
  127. setCellValue(sheet, 4 + i, 1, bisInspPlanChk.getChkMatter());
  128. setCellValue(sheet, 4 + i, 2, bisInspPlanChk.getChkBatch());
  129. setCellValue(sheet, 4 + i, 3, bisInspPlanChk.getChkTm());
  130. setCellValue(sheet, 4 + i, 4, bisInspPlanChk.getChkCity());
  131. setCellValue(sheet, 4 + i, 5, bisInspPlanChk.getChkCounty());
  132. setCellValue(sheet, 4 + i, 6, bisInspPlanChk.getChkItem());
  133. setCellValue(sheet, 4 + i, 7, bisInspPlanChk.getChkContent());
  134. setCellValue(sheet, 4 + i, 8, bisInspPlanChk.getChkDep());
  135. setCellValue(sheet, 4 + i, 9, bisInspPlanChk.getChkType());
  136. setCellValue(sheet, 4 + i, 10, "");
  137. }
  138. private void setCellValue(XSSFSheet sheet, int rownum, int cellnum, String value) {
  139. XSSFCell cell = sheet.getRow(rownum).getCell(cellnum);
  140. cell.setCellValue(value);
  141. }
  142. /**
  143. * 导出PDF
  144. *
  145. * @param response
  146. * @param wb
  147. * @param name
  148. * @throws IOException
  149. */
  150. private void writeToResponse(HttpServletResponse response, XSSFWorkbook wb, String name) throws IOException {
  151. // 修改模板内容导出新模板
  152. response.setContentType("application/x-msdownload");
  153. String fileNameURL = URLEncoder.encode(name, "UTF-8");
  154. response.setHeader("Content-disposition", "attachment;filename=" + fileNameURL + ";filename*=utf-8''" + fileNameURL);
  155. OutputStream out = response.getOutputStream();
  156. wb.write(out);
  157. out.flush();
  158. out.close();
  159. }
  160. }