package cn.com.goldenwater.dcproj.service.impl.ducha; import cn.com.goldenwater.core.service.AbstractCrudService; import cn.com.goldenwater.dcproj.dao.BisInspPlanChkDao; import cn.com.goldenwater.dcproj.model.BisInspPlanChk; import cn.com.goldenwater.dcproj.param.BisInspPlanChkParam; import cn.com.goldenwater.dcproj.service.BisInspPlanChkService; import cn.com.goldenwater.id.util.UuidUtil; import org.apache.commons.lang3.StringUtils; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.xssf.usermodel.XSSFCell; import org.apache.poi.xssf.usermodel.XSSFCellStyle; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import javax.servlet.http.HttpServletResponse; import java.io.*; import java.net.URLEncoder; import java.util.Date; import java.util.List; /** * @author lhc * @date 2021-4-1 */ @Service @Transactional public class BisInspPlanChkServiceImpl extends AbstractCrudService implements BisInspPlanChkService { @Value("${export.templatePath}") private String templatePath; @Autowired private BisInspPlanChkDao bisInspPlanChkDao; public BisInspPlanChkServiceImpl(BisInspPlanChkDao bisInspPlanChkDao) { super(bisInspPlanChkDao); this.bisInspPlanChkDao = bisInspPlanChkDao; } @Override public int insert(BisInspPlanChk bisInspPlanChk) { String uuid = UuidUtil.uuid(); // 生成uuid bisInspPlanChk.setId(uuid); bisInspPlanChk.setIntm(new Date()); bisInspPlanChk.setUptm(new Date()); bisInspPlanChk.setDataStat("0"); return this.bisInspPlanChkDao.insert(bisInspPlanChk); } @Override public int update(BisInspPlanChk bisInspPlanChk) { bisInspPlanChk.setUptm(new Date()); return this.bisInspPlanChkDao.update(bisInspPlanChk); } @Override public int delete(String id) { return this.bisInspPlanChkDao.delete(id); } @Override public void exportCheckList(HttpServletResponse response, BisInspPlanChkParam bisInspPlanChkParam) { String templateFilePath = templatePath + File.separator + "jdjcjh2.xlsx"; if (StringUtils.isNotBlank(bisInspPlanChkParam.getChkDep())) { templateFilePath = templatePath + File.separator + "jdjcjh1.xlsx"; } File fi = new File(templateFilePath); try (InputStream fs = new FileInputStream(fi); XSSFWorkbook wb = new XSSFWorkbook(fs)) { // 2.数据赋值 updateCheckList(wb, bisInspPlanChkParam); // 3.导出xlsx String title = bisInspPlanChkParam.getChkYear() + "年度水利强监管工作计划表.xlsx"; if (StringUtils.isNotBlank(bisInspPlanChkParam.getChkDep())) { title = bisInspPlanChkParam.getChkDep() + bisInspPlanChkParam.getChkYear() + "年度水利强监管工作计划表.xlsx"; } writeToResponse(response, wb, title); } catch (IOException e) { e.printStackTrace(); } } private void updateCheckList(XSSFWorkbook wb, BisInspPlanChkParam bisInspPlanChkParam) { // 1.获取数据 List checkListData = bisInspPlanChkDao.findList(bisInspPlanChkParam); // 读取了模板内所有sheet内容 XSSFSheet sheet = wb.getSheetAt(0); String title = checkListData.get(0).getChkYear() + "年度水利强监管工作计划表"; if (StringUtils.isNotBlank(bisInspPlanChkParam.getChkDep())) { title = bisInspPlanChkParam.getChkDep() + checkListData.get(0).getChkYear() + "年度水利强监管工作计划表"; } // 在相应的单元格进行赋值 setCellValue(sheet, 1, 0, title); // 第 3 行开始录入问题 XSSFCellStyle cellStyle = sheet.getRow(3).getCell(0).getCellStyle(); for (int i = 0; i < checkListData.size(); i++) { // if (i > 0) { // 如果不是第一行,创建行 Row row = sheet.createRow(4 + i); int lie = 9; if (StringUtils.isNotBlank(bisInspPlanChkParam.getChkDep())) { lie = 11; } for (int j = 0; j < lie; j++) { Cell cell = row.createCell(j); cell.setCellStyle(cellStyle); } // } } for (int i = 0; i < checkListData.size(); i++) { BisInspPlanChk bisInspPlanChk = checkListData.get(i); if (StringUtils.isNotBlank(bisInspPlanChkParam.getChkDep())) { setDepCell(sheet, i, bisInspPlanChk); } else { setAllCell(sheet, i, bisInspPlanChk); } } } private void setAllCell(XSSFSheet sheet, int i, BisInspPlanChk bisInspPlanChk) { setCellValue(sheet, 4 + i, 0, String.valueOf(i + 1)); setCellValue(sheet, 4 + i, 1, bisInspPlanChk.getChkMatter()); setCellValue(sheet, 4 + i, 2, bisInspPlanChk.getChkCity()); setCellValue(sheet, 4 + i, 3, bisInspPlanChk.getChkCounty()); setCellValue(sheet, 4 + i, 4, bisInspPlanChk.getChkContent()); setCellValue(sheet, 4 + i, 5, bisInspPlanChk.getChkDep()); setCellValue(sheet, 4 + i, 6, bisInspPlanChk.getChkType()); setCellValue(sheet, 4 + i, 7, bisInspPlanChk.getChkTm()); setCellValue(sheet, 4 + i, 8, ""); } private void setDepCell(XSSFSheet sheet, int i, BisInspPlanChk bisInspPlanChk) { setCellValue(sheet, 4 + i, 0, String.valueOf(i + 1)); setCellValue(sheet, 4 + i, 1, bisInspPlanChk.getChkMatter()); setCellValue(sheet, 4 + i, 2, bisInspPlanChk.getChkBatch()); setCellValue(sheet, 4 + i, 3, bisInspPlanChk.getChkTm()); setCellValue(sheet, 4 + i, 4, bisInspPlanChk.getChkCity()); setCellValue(sheet, 4 + i, 5, bisInspPlanChk.getChkCounty()); setCellValue(sheet, 4 + i, 6, bisInspPlanChk.getChkItem()); setCellValue(sheet, 4 + i, 7, bisInspPlanChk.getChkContent()); setCellValue(sheet, 4 + i, 8, bisInspPlanChk.getChkDep()); setCellValue(sheet, 4 + i, 9, bisInspPlanChk.getChkType()); setCellValue(sheet, 4 + i, 10, ""); } private void setCellValue(XSSFSheet sheet, int rownum, int cellnum, String value) { XSSFCell cell = sheet.getRow(rownum).getCell(cellnum); cell.setCellValue(value); } /** * 导出PDF * * @param response * @param wb * @param name * @throws IOException */ private void writeToResponse(HttpServletResponse response, XSSFWorkbook wb, String name) throws IOException { // 修改模板内容导出新模板 response.setContentType("application/x-msdownload"); String fileNameURL = URLEncoder.encode(name, "UTF-8"); response.setHeader("Content-disposition", "attachment;filename=" + fileNameURL + ";filename*=utf-8''" + fileNameURL); OutputStream out = response.getOutputStream(); wb.write(out); out.flush(); out.close(); } }