diff --git a/ruoyi-admin/pom.xml b/ruoyi-admin/pom.xml index 3cb5b52..6cdb052 100644 --- a/ruoyi-admin/pom.xml +++ b/ruoyi-admin/pom.xml @@ -24,7 +24,7 @@ cn.hutool hutool-all - 5.8.23 + 5.8.37 org.jsoup @@ -118,7 +118,13 @@ knife4j-openapi2-spring-boot-starter 4.4.0 - + + org.apache.commons + commons-compress + 1.21 + compile + + diff --git a/ruoyi-admin/src/main/java/com/ruoyi/gysl/controller/BasicInformationController.java b/ruoyi-admin/src/main/java/com/ruoyi/gysl/controller/BasicInformationController.java index 63b37d5..fb35ffe 100644 --- a/ruoyi-admin/src/main/java/com/ruoyi/gysl/controller/BasicInformationController.java +++ b/ruoyi-admin/src/main/java/com/ruoyi/gysl/controller/BasicInformationController.java @@ -1,6 +1,9 @@ package com.ruoyi.gysl.controller; - +import cn.hutool.core.collection.CollUtil; +import cn.hutool.core.util.ZipUtil; +import cn.hutool.core.io.FileUtil; +import cn.hutool.core.io.IoUtil; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.ruoyi.common.core.controller.BaseController; import com.ruoyi.common.core.domain.AjaxResult; @@ -22,11 +25,15 @@ import org.springframework.transaction.annotation.Transactional; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; + import javax.annotation.Resource; import javax.servlet.http.HttpServletResponse; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; +import javax.validation.constraints.Size; +import java.io.*; +import java.util.*; +import java.util.zip.ZipEntry; + +import static java.lang.System.out; /** * 基本信息(BasicInformation)表控制层 @@ -140,7 +147,7 @@ public class BasicInformationController extends BaseController { if (byId.getStatus() != 1) { throw new ServiceException("暂不能提交!"); } - return success(basicInformationService.audit(req, 2,byId.getStatus())); + return success(basicInformationService.audit(req, 2, byId.getStatus())); } /** @@ -154,7 +161,7 @@ public class BasicInformationController extends BaseController { if (byId.getStatus() == 1) { throw new ServiceException("请等待企业填报!"); } - return success(basicInformationService.audit(req, 3,byId.getStatus())); + return success(basicInformationService.audit(req, 3, byId.getStatus())); } /** @@ -261,7 +268,7 @@ public class BasicInformationController extends BaseController { // @PreAuthorize("@ss.hasAnyRoles('admin,common')") @ApiOperation("导出项目数据") @PostMapping("/exportInfo") - public void exportInfo(HttpServletResponse response){ + public void exportInfo(HttpServletResponse response) { List pE = basicInformationService.selectList(); ExcelUtil util = new ExcelUtil<>(ProjectExcelInfo.class); util.exportExcel(response, pE, "项目数据"); @@ -292,7 +299,8 @@ public class BasicInformationController extends BaseController { } // @ApiOperation(value = "123") -//// @PreAuthorize("@ss.hasAnyRoles('admin,common')") + + /// / @PreAuthorize("@ss.hasAnyRoles('admin,common')") // @PostMapping(value = "/import1", consumes = "multipart/form-data") // @Transactional(rollbackFor = Exception.class) // public AjaxResult import1(@RequestPart("file") MultipartFile file) throws Exception { @@ -329,5 +337,161 @@ public class BasicInformationController extends BaseController { // }); // return AjaxResult.success(); // } + @ApiOperation(value = "导出word") + @PostMapping("/wordExport") + public void wordExport(HttpServletResponse response, @RequestBody List ids) throws Exception { + // 创建临时 ZIP 文件 + java.io.File tempZipFile = java.io.File.createTempFile("archive", ".zip"); + + // 设置文件 + try { + List fileList = CollUtil.newArrayList(); + for (long id : ids) { + BasicInformationResponse a1 = basicInformationService.selectOne(id); + Map map = new HashMap<>(); + map.put("name", a1.getBasicInformation().getName() != null ? a1.getBasicInformation().getName() : "/"); + map.put("ssgnq", a1.getBasicInformation().getSsgnq() != null ? a1.getBasicInformation().getSsgnq() : "/"); + map.put("jsdd", a1.getBasicInformation().getJsdd() != null ? a1.getBasicInformation().getJsdd() : "/"); + map.put("xmfrdwxz", a1.getBasicInformation().getXmfrdwxz() != null ? a1.getBasicInformation().getXmfrdwxz() : "/"); + map.put("tyshxydm", a1.getBasicInformation().getTyshxydm() != null ? a1.getBasicInformation().getTyshxydm() : "/"); + map.put("sgdw", a1.getBasicInformation().getSgdw() != null ? a1.getBasicInformation().getSgdw() : "/"); + map.put("sjdw", a1.getBasicInformation().getSjdw() != null ? a1.getBasicInformation().getSjdw() : "/"); + map.put("begainTime", a1.getBasicInformation().getBegainTime() != null ? a1.getBasicInformation().getBegainTime() : "/"); + map.put("endTime", a1.getBasicInformation().getEndTime() != null ? a1.getBasicInformation().getEndTime() : "/"); + map.put("prioritize",a1.getBasicInformation().getPrioritize() != null ? a1.getBasicInformation().getPrioritize() : "/"); + map.put("label",a1.getBasicInformation().getLabel() != null ? a1.getBasicInformation().getLabel() : "/"); + map.put("projectLeader", a1.getBasicInformation().getProjectLeader() != null ? a1.getBasicInformation().getProjectLeader() : "/"); + map.put("phone", a1.getBasicInformation().getPhone() != null ? a1.getBasicInformation().getPhone() : "/"); + map.put("zydmj", a1.getBasicInformation().getZydmj() != null ? a1.getBasicInformation().getZydmj() : "/"); + if(a1.getPlanInformation()!=null) { + map.put("rjl", a1.getPlanInformation().getRjl() != null ? a1.getPlanInformation().getRjl() : "/"); + map.put("zjzmj", a1.getPlanInformation().getZjzmj() != null ? a1.getPlanInformation().getZjzmj() : "/"); + map.put("jzds", a1.getPlanInformation().getJzds() != null ? a1.getPlanInformation().getJzds() : "/"); + map.put("bzcjzmj",a1.getPlanInformation().getBzcjzmj() !=null ? a1.getPlanInformation().getBzcjzmj() :"/"); + map.put("bzczmj", a1.getPlanInformation().getBzcjzmj() != null ? a1.getPlanInformation().getBzcjzmj() : "/"); + map.put("jzmd", a1.getPlanInformation().getJzmd() != null ? a1.getPlanInformation().getJzmd() : "/"); + map.put("zgjzcs", a1.getPlanInformation().getZgjzcs() != null ? a1.getPlanInformation().getZgjzcs() : "/"); + map.put("zgjzgd", a1.getPlanInformation().getZgjzgd() != null ? a1.getPlanInformation().getZgjzgd() : "/"); + map.put("fhdj", a1.getPlanInformation().getFhdj() != null ? a1.getPlanInformation().getFhdj() : "/"); + map.put("jdctcw", a1.getPlanInformation().getJdctcw() != null ? a1.getPlanInformation().getJdctcw() : "/"); + map.put("fjdctcw", a1.getPlanInformation().getFjdctcw() != null ? a1.getPlanInformation().getFjdctcw() : "/"); + } + else{ + map.put("rjl","/"); + map.put("zjzmj","/"); + map.put("jzds","/"); + map.put("bzcjzmj","/"); + map.put("bzczmj","/"); + map.put("jzmd","/"); + map.put("zgjzcs","/"); + map.put("zgjzgd","/"); + map.put("fhdj","/"); + map.put("jdctcw","/"); + map.put("fjdctcw","/"); + } + if (a1.getBasicInformation().getNature() != null) { + switch (a1.getBasicInformation().getNature()) { + case 1: + map.put("nature", "国企"); + break; + case 2: + map.put("nature", "外资企业"); + break; + case 3: + map.put("nature", "民营企业"); + break; + case 4: + map.put("nature", "其他"); + break; + default: + map.put("nature", "/"); + } + } else { + map.put("nature", "/"); + } + map.put("jhtze", a1.getBasicInformation().getJhtze() != null ? a1.getBasicInformation().getJhtze() : "/"); + map.put("ztze", a1.getBasicInformation().getZtze() != null ? a1.getBasicInformation().getZtze() : "/"); + if (a1.getBasicInformation().getXzfl() != null) { + switch (a1.getBasicInformation().getXzfl()) { + case 1: + map.put("xzfl", "已建"); + break; + case 2: + map.put("xzfl", "在建"); + break; + case 3: + map.put("xzfl", "拟建"); + break; + default: + map.put("xzfl", "/"); + } + } else { + map.put("xzfl", "/"); + } + + map.put("begainTime1", a1.getBasicInformation().getBegainTime() != null ? a1.getBasicInformation().getBegainTime() : "/"); + map.put("jsjd", (a1.getBasicInformation().getJsjd() != null) + ? a1.getBasicInformation().getJsjd() + : "/"); + if (a1.getQyrzInformation()!=null) { + map.put("rzqys", (a1.getQyrzInformation().getRzqys() != null) ? a1.getQyrzInformation().getRzqys() : "/"); + map.put("rzqyhylx", a1.getQyrzInformation().getRzqyhylx() != null ? a1.getQyrzInformation().getRzqyhylx() : "/"); + map.put("rysl", a1.getQyrzInformation().getRysl() != null ? a1.getQyrzInformation().getRysl() : "/"); + map.put("rzl", a1.getQyrzInformation().getRzl() != null ? a1.getQyrzInformation().getRzl() : "/"); + map.put("yczmj", a1.getQyrzInformation().getYczmj() != null ? a1.getQyrzInformation().getYczmj() : "/"); + map.put("kzczmj",a1.getQyrzInformation().getKzczmj() != null ? a1.getQyrzInformation().getKzczmj() : "/"); + map.put("gycfpjzj", a1.getQyrzInformation().getGycfpjzj() != null ? a1.getQyrzInformation().getGycfpjzj() : "/"); + }else{ + map.put("rzqys","/"); + map.put("rzqyhylx","/"); + map.put("rysl","/"); + map.put("rzl","/"); + map.put("yczmj","/"); + map.put("kzczmj","/"); + map.put("gycfpjzj","/"); + } + File wordFile = new File(a1.getBasicInformation().getName() + ".docx"); + try (InputStream inputStream = basicInformationService.generateDocument(map); + OutputStream outputStream = new FileOutputStream(a1.getBasicInformation().getName() + ".docx")) { + if (inputStream != null) { + byte[] buffer = new byte[4096]; + int bytesRead; + while ((bytesRead = inputStream.read(buffer)) != -1) { + outputStream.write(buffer, 0, bytesRead); + } + } + + } catch (IOException e) { + e.printStackTrace(); + } + fileList.add(wordFile); + } + // 将生成的 Word 文件添加到 ZIP 文件中 + ZipUtil.zip(tempZipFile, false, fileList.toArray(new File[fileList.size()])); + // 设置响应头 + response.setContentType("application/x-msdownload"); + response.setHeader("Content-Disposition", "attachment; filename=archive.zip"); + + // 将 ZIP 文件内容写入响应输出流 + try (InputStream zipInputStream = FileUtil.getInputStream(tempZipFile); + OutputStream responseOutputStream = response.getOutputStream()) { + IoUtil.copy(zipInputStream, responseOutputStream); + } + } + finally { + // 删除临时 ZIP 文件 + if (tempZipFile.exists()) { + FileUtil.del(tempZipFile); + } + // 清理生成的 Word 文件 + for (long id : ids) { + BasicInformationResponse a1 = basicInformationService.selectOne(id); + File wordFile = new File(a1.getBasicInformation().getName() + ".docx"); + if (wordFile.exists()) { + FileUtil.del(wordFile); + } + } + } + } } diff --git a/ruoyi-admin/src/main/java/com/ruoyi/gysl/service/BasicInformationService.java b/ruoyi-admin/src/main/java/com/ruoyi/gysl/service/BasicInformationService.java index cac6413..d9db05f 100644 --- a/ruoyi-admin/src/main/java/com/ruoyi/gysl/service/BasicInformationService.java +++ b/ruoyi-admin/src/main/java/com/ruoyi/gysl/service/BasicInformationService.java @@ -6,11 +6,9 @@ import com.ruoyi.gysl.entity.BasicInformation; import com.ruoyi.gysl.entity.request.AuditRequest; import com.ruoyi.gysl.entity.request.BasicInformationPageReq; import com.ruoyi.gysl.entity.response.BasicInformationResponse; -import com.ruoyi.gysl.entity.response.ProjectBuildingExcel; import com.ruoyi.gysl.entity.response.ProjectExcelInfo; -import org.apache.poi.xssf.usermodel.XSSFCellStyle; -import org.apache.poi.xssf.usermodel.XSSFSheet; +import java.io.ByteArrayInputStream; import java.io.Serializable; import java.util.List; import java.util.Map; @@ -94,6 +92,6 @@ public interface BasicInformationService extends IService { */ BasicInformationResponse stagingInfo(Long id); - void generateDocument(String filePath, Map data)throws Exception; + ByteArrayInputStream generateDocument(Map map)throws Exception; } diff --git a/ruoyi-admin/src/main/java/com/ruoyi/gysl/service/impl/BasicInformationServiceImpl.java b/ruoyi-admin/src/main/java/com/ruoyi/gysl/service/impl/BasicInformationServiceImpl.java index 3b0b7ff..3a32af2 100644 --- a/ruoyi-admin/src/main/java/com/ruoyi/gysl/service/impl/BasicInformationServiceImpl.java +++ b/ruoyi-admin/src/main/java/com/ruoyi/gysl/service/impl/BasicInformationServiceImpl.java @@ -3,7 +3,6 @@ package com.ruoyi.gysl.service.impl; import cn.hutool.core.io.resource.ClassPathResource; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; -import com.ruoyi.common.annotation.Excel; import com.ruoyi.common.exception.ServiceException; import com.ruoyi.common.utils.SecurityUtils; import com.ruoyi.docking.entity.SmartDeclaration; @@ -15,14 +14,9 @@ import com.ruoyi.gysl.entity.request.BasicInformationPageReq; import com.ruoyi.gysl.entity.request.RemarkPageReq; import com.ruoyi.gysl.entity.request.ZwIdPageReq; import com.ruoyi.gysl.entity.response.BasicInformationResponse; -import com.ruoyi.gysl.entity.response.ProjectBuildingExcel; import com.ruoyi.gysl.entity.response.ProjectExcelInfo; import com.ruoyi.gysl.mapper.BasicInformationMapper; import com.ruoyi.gysl.service.*; -import org.apache.poi.xssf.usermodel.XSSFCell; -import org.apache.poi.xssf.usermodel.XSSFCellStyle; -import org.apache.poi.xssf.usermodel.XSSFRow; -import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.apache.poi.xwpf.usermodel.XWPFParagraph; import org.apache.poi.xwpf.usermodel.XWPFRun; @@ -30,10 +24,10 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; -import java.io.FileOutputStream; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.io.Serializable; -import java.lang.reflect.Field; import java.util.List; import java.util.Map; import java.util.Objects; @@ -129,7 +123,6 @@ public class BasicInformationServiceImpl extends ServiceImpl collect1 = req.getWysmxInformations().stream().filter(x -> @@ -322,12 +315,12 @@ public class BasicInformationServiceImpl extends ServiceImpl data) throws Exception { + public ByteArrayInputStream generateDocument(Map map) throws Exception { // 加载模板文件 - try (InputStream inputStream = new ClassPathResource("template.docx").getStream()) { + // InputStream inputStream = new ClassPathResource("xmdpcl.docx").getStream() + ClassPathResource classPathResource = new ClassPathResource("xmdpcl.docx"); + try (InputStream inputStream = classPathResource.getStream()) { + XWPFDocument document = new XWPFDocument(inputStream); // 替换模板中的占位符 for (XWPFParagraph paragraph : document.getParagraphs()) { - for (String key : data.keySet()) { - String text = paragraph.getText(); - if (text.contains("{" + key + "}")) { - text = text.replace("{" + key + "}", data.get(key)); - paragraph.removeRun(0); // 删除原有内容 + for (String key : map.keySet()) { + StringBuilder textBuilder = new StringBuilder(); + for (XWPFRun run : paragraph.getRuns()) { + if (run != null && run.getText(0) != null) { + textBuilder.append(run.getText(0)); + } + } + String text = textBuilder.toString(); + if (text.contains("${" + key + "}")) { + text = text.replace("${" + key + "}", map.get(key).toString()); + // 删除原有内容 + while (paragraph.getRuns().size() > 0) { + paragraph.removeRun(0); + } XWPFRun run = paragraph.createRun(); run.setText(text); // 设置替换后的内容 } } } - // 输出生成的文档 - try (FileOutputStream out = new FileOutputStream(filePath)) { - document.write(out); + // 将文档内容写入 ByteArrayOutputStream + try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream()) { + document.write(byteArrayOutputStream); + // 将 ByteArrayOutputStream 转换为 ByteArrayInputStream 并返回 + return new ByteArrayInputStream(byteArrayOutputStream.toByteArray()); } } } } + diff --git a/ruoyi-admin/src/main/resources/static/word/项目单片材料模板.dotx b/ruoyi-admin/src/main/resources/static/word/项目单片材料模板.dotx deleted file mode 100644 index 11760a7..0000000 --- a/ruoyi-admin/src/main/resources/static/word/项目单片材料模板.dotx +++ /dev/null @@ -1,16 +0,0 @@ - - {{name}}项目单片材料 - - 投资主体简介 - 该项目位于{{ssgnq}},建设地点位于{{jsdd}},由{{xmfrdwxz}} -有限公司,统一信用代码为{{tyshxydm}}投资,由{{sgdw}}有限公司施工,设计单位是{{sjdw}}有限公司,建设起止时间于{{begainTime}} - {{endTime}},其重点发展产业为{{prioritize}},该项目的项目标签是{{label}},整体的项目由{{projectLeader}}负责,联系方式是{{phone}}。 - 规划信息 -该项目总用地面积约{{zydmj}}平方米,容积率占{{rjl}},总建筑面积为{{zjzmj}}万平方米,共有{{jzds}}栋,标准建筑面积为{{bzcjzmj}}平方米,建筑密度为{{jzmd}}%,最高建筑层数{{zgjzcs}}层,最高建筑高度{{zgjzgd}}米,其防火等级为{{fhdj}},机动车停车位{{jdctcw}}辆和非机动车听车辆{{fjdctcw}}辆。 - 计划总投资及建设计划 - 该项目投资主体性质是{{nature}},计划投资额度xxx亿元,实际总投资额{{ztze}}万人民币。 - 当前进展情况 - 目前,该项目属于{{xzfl}},于 {{begainTime}}日正式施工,截至目前{{jsjd}}。 - 企业运营情况 - 目前,该项目入驻企业数量共{{rzqys}}家,入驻企业行业类型为{{rzqyhylx}},人员数量共计{{eysl}}人,入住率为{{rzl}}%,其中已出租面积{{yczmj}}平方米,空置房屋{{kzczmj}}万平方米,工业厂房平均租金{{gycfpjzj}}元/平方米*月。 - 存在困难问题 - 暂无。