From d433306dd16e228d0661e7b6eae328d3a41adb6a Mon Sep 17 00:00:00 2001 From: zhn <947514737@qq.com> Date: Thu, 7 Jul 2022 08:53:39 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9C=88=E6=8A=A5=E6=95=B4=E5=90=88=E5=AF=BC?= =?UTF-8?q?=E5=87=BA(=E4=B8=B4=E6=97=B6)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../LawsMonthlyReportWriteEOController.java | 12 +++ .../entity/LawsMonthlyReportWriteEO.java | 4 + .../ILawsMonthlyReportWriteEOService.java | 3 + .../LawsMonthlyReportWriteEOServiceImpl.java | 83 ++++++++++++++++++ .../jero/modules/report/util/WordUtil.java | 87 +++++++++++++++++++ 5 files changed, 189 insertions(+) create mode 100644 jero-boot/jero-boot-modules/src/main/java/com/jero/modules/report/util/WordUtil.java diff --git a/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/report/controller/LawsMonthlyReportWriteEOController.java b/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/report/controller/LawsMonthlyReportWriteEOController.java index aef3b081d..8c873e5d6 100644 --- a/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/report/controller/LawsMonthlyReportWriteEOController.java +++ b/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/report/controller/LawsMonthlyReportWriteEOController.java @@ -10,6 +10,7 @@ import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.ResponseEntity; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; @@ -22,6 +23,7 @@ import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -172,4 +174,14 @@ public class LawsMonthlyReportWriteEOController extends JeroController exportMonthlyReport(LawsMonthlyReportWriteEO lawsMonthlyReportWriteEO) throws IOException { + return lawsMonthlyReportWriteEOService.exportMonthlyReport(lawsMonthlyReportWriteEO); + } + } diff --git a/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/report/entity/LawsMonthlyReportWriteEO.java b/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/report/entity/LawsMonthlyReportWriteEO.java index 16af6ede5..d5244a469 100644 --- a/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/report/entity/LawsMonthlyReportWriteEO.java +++ b/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/report/entity/LawsMonthlyReportWriteEO.java @@ -187,4 +187,8 @@ public class LawsMonthlyReportWriteEO implements Serializable { @TableField(exist = false) private String cut; + //导出 + @TableField(exist = false) + private String ids; + } diff --git a/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/report/service/ILawsMonthlyReportWriteEOService.java b/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/report/service/ILawsMonthlyReportWriteEOService.java index 82c461d68..503feabb0 100644 --- a/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/report/service/ILawsMonthlyReportWriteEOService.java +++ b/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/report/service/ILawsMonthlyReportWriteEOService.java @@ -3,6 +3,7 @@ package com.jero.modules.report.service; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.service.IService; import com.jero.modules.report.entity.LawsMonthlyReportWriteEO; +import org.springframework.http.ResponseEntity; import javax.servlet.http.HttpServletRequest; import java.util.List; @@ -74,4 +75,6 @@ public interface ILawsMonthlyReportWriteEOService extends IService exportMonthlyReport(LawsMonthlyReportWriteEO lawsMonthlyReportWriteEO); } diff --git a/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/report/service/impl/LawsMonthlyReportWriteEOServiceImpl.java b/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/report/service/impl/LawsMonthlyReportWriteEOServiceImpl.java index 8906e352a..60deb8c14 100644 --- a/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/report/service/impl/LawsMonthlyReportWriteEOServiceImpl.java +++ b/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/report/service/impl/LawsMonthlyReportWriteEOServiceImpl.java @@ -18,13 +18,21 @@ import com.jero.modules.report.service.ILawsMonthlyReportTitleTemplateEOService; import com.jero.modules.report.service.ILawsMonthlyReportWriteEOService; import com.jero.modules.report.service.INewOpinionTemplateEOService; import com.jero.modules.report.service.INewStandardTemplateEOService; +import com.jero.modules.report.util.WordUtil; import com.jero.modules.system.entity.SysCategory; import com.jero.modules.system.service.impl.SysCategoryServiceImpl; import com.jero.modules.system.util.StringUtils; +import org.apache.poi.xwpf.usermodel.ParagraphAlignment; +import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Service; import javax.servlet.http.HttpServletRequest; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.Date; @@ -48,6 +56,8 @@ public class LawsMonthlyReportWriteEOServiceImpl extends ServiceImpl exportMonthlyReport(LawsMonthlyReportWriteEO lawsMonthlyReportWriteEO) { + ResponseEntity result = null; + //获取数据 + LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>(); + if(StringUtils.isNotBlank(lawsMonthlyReportWriteEO.getMonth())){ + wrapper.eq(LawsMonthlyReportWriteEO::getMonth,lawsMonthlyReportWriteEO.getMonth()); + } + if(StringUtils.isNotBlank(lawsMonthlyReportWriteEO.getIds())){ + wrapper.in(LawsMonthlyReportWriteEO::getId,StringUtils.join(lawsMonthlyReportWriteEO.getIds(),"")); + } + List lawsMonthlyReportWriteEOS = this.list(wrapper); + //月报填写列表所有的章节目录 + List memoriesChapterList = lawsMonthlyReportWriteEOS.stream() + .map(LawsMonthlyReportWriteEO::getMemoriesChapter).distinct().collect(Collectors.toList()); + + //所有的章节目录 + List lawsMonthlyReportTitleTemplateEOS = lawsMonthlyReportTitleTemplateEOService.list(); + lawsMonthlyReportTitleTemplateEOS = lawsMonthlyReportTitleTemplateEOS.stream() + .filter(e -> memoriesChapterList.contains(e.getId())).collect(Collectors.toList()); + + String path = uploadpath + "/tempZip"; + File fileTemp = new File(path); + if (fileTemp.exists()) { + fileTemp.delete(); + } + fileTemp.mkdirs(); + createWord(path,lawsMonthlyReportWriteEOS,lawsMonthlyReportTitleTemplateEOS); + + + + + return null; + } + + /** + * + * @param path + * @param lawsMonthlyReportWriteEOS 列表数据 + * @param lawsMonthlyReportTitleTemplateEOS 章节目录 + */ + private void createWord(String path, + List lawsMonthlyReportWriteEOS, + List lawsMonthlyReportTitleTemplateEOS){ + //蔚来汽车法规月报-202004-主页面-CN + //NIO Monthly Report on Automobile Regulations-202004-Main Page-EN + String memoriesChapterCN = path + File.separator +"蔚来汽车法规月报" + lawsMonthlyReportWriteEOS.get(0).getMonth()+"主页面-CN.docx"; + String memoriesChapterEN = path + File.separator +"NIO Monthly Report on Automobile Regulations-" + lawsMonthlyReportWriteEOS.get(0).getMonth()+"-Main Page-EN.docx"; + + File wordFile = new File(memoriesChapterCN); + if(!wordFile.exists()){ + try { + wordFile.getParentFile().mkdirs(); + wordFile.createNewFile(); + } catch (IOException e) { + log.error("文件《"+memoriesChapterCN+"》创建失败"); + } + } + + try (XWPFDocument document = new XWPFDocument(); + FileOutputStream out = new FileOutputStream(wordFile)) { + WordUtil.exportWord(document, memoriesChapterCN, null, ParagraphAlignment.CENTER, 0, 12, false, true, "Microsoft YaHei UI"); + + + + } catch (IOException e) { + log.error("文件创建异常,异常信息为:" + e.getMessage()); + } + + + + } } diff --git a/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/report/util/WordUtil.java b/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/report/util/WordUtil.java new file mode 100644 index 000000000..607d5da73 --- /dev/null +++ b/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/report/util/WordUtil.java @@ -0,0 +1,87 @@ +package com.jero.modules.report.util; + +import cn.hutool.core.util.ObjectUtil; +import org.apache.commons.lang.StringUtils; +import org.apache.poi.xwpf.usermodel.ParagraphAlignment; +import org.apache.poi.xwpf.usermodel.XWPFDocument; +import org.apache.poi.xwpf.usermodel.XWPFParagraph; +import org.apache.poi.xwpf.usermodel.XWPFRun; + +public class WordUtil { + /** + * 导出工具示例 + * + * @param document POI文件类 + * @param text 输出文本 + * @param textColor 字体颜色 + * @param align 字体位置,默认居左 + * @param spaceNum 预留空格数,默认不留空格 + * @param fontSize 字体大小,默认11 + * @param isNewline 是否换行,换行后文本失效 + * @param isBold 是否加粗,默认不加粗 + * @param fontFamily 字体,默认宋体 + */ + public static void exportWord(XWPFDocument document, + String text, + String textColor, + ParagraphAlignment align, + Integer spaceNum, + Integer fontSize, + Boolean isNewline, + Boolean isBold, + String fontFamily) { + + document.createParagraph(); + + //添加标题 + XWPFParagraph titleParagraph = document.createParagraph(); + //设置段落左对齐 + if (align == null) { + titleParagraph.setAlignment(ParagraphAlignment.LEFT); + } else { + titleParagraph.setAlignment(align); + } + + XWPFRun titleParagraphRun = titleParagraph.createRun(); + if (isNewline) { + titleParagraphRun.setText("\r"); + } else { + //封装文字内容 + StringBuilder newText = new StringBuilder(); + for (int i = 0; i < spaceNum; i++) { + newText.append(" "); + } + newText.append(text); + titleParagraphRun.setText(newText.toString()); + + //设置颜色 + if (StringUtils.isNotEmpty(textColor)) { + titleParagraphRun.setColor(textColor); + } else { + titleParagraphRun.setColor("000000"); + } + //设置文字大小 + if (ObjectUtil.isNotEmpty(fontSize)) { + titleParagraphRun.setFontSize(fontSize); + } else { + titleParagraphRun.setFontSize(11); + } + + //设置加粗 + if (ObjectUtil.isNotEmpty(isBold)) { + titleParagraphRun.setBold(isBold); + } else { + titleParagraphRun.setBold(false); + } + + //设置字体样式 + if (StringUtils.isNotEmpty(fontFamily)) { + titleParagraphRun.setFontFamily(fontFamily); + } else { + titleParagraphRun.setFontFamily("宋体"); + } + + } + } +} +