From 2012221b7316f585979aa4948a7b51762652e82e Mon Sep 17 00:00:00 2001 From: hongawen <83944980@qq.com> Date: Tue, 2 Dec 2025 13:37:35 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B2=B3=E5=8C=97=E6=8A=A5=E5=91=8A=E5=AE=9A?= =?UTF-8?q?=E5=88=B6=E5=8C=96=E6=94=B9=E5=8A=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/impl/PqReportServiceImpl.java | 32 ++++++-- .../gather/report/utils/BookmarkUtil.java | 25 ++++--- .../njcn/gather/report/utils/Docx4jUtil.java | 73 ++++++++++++------- 3 files changed, 86 insertions(+), 44 deletions(-) diff --git a/detection/src/main/java/com/njcn/gather/report/service/impl/PqReportServiceImpl.java b/detection/src/main/java/com/njcn/gather/report/service/impl/PqReportServiceImpl.java index 881647b9..fd2c83da 100644 --- a/detection/src/main/java/com/njcn/gather/report/service/impl/PqReportServiceImpl.java +++ b/detection/src/main/java/com/njcn/gather/report/service/impl/PqReportServiceImpl.java @@ -1232,16 +1232,34 @@ public class PqReportServiceImpl extends ServiceImpl i Tr existingRow = (Tr) tbl.getContent().get(rows.size() - 1); // 获取现有样式 TrPr trPr = existingRow.getTrPr(); - JAXBElement element = (JAXBElement) existingRow.getContent().get(0); - TcPr tcPr = element.getValue().getTcPr(); - TblWidth cellWidth = factory.createTblWidth(); - cellWidth.setType("dxa"); - cellWidth.setW(BigInteger.valueOf(5000 / tableKeys.size())); - tcPr.setTcW(cellWidth); + // 获取每个单元格的TcPr(保留各单元格独立的边框设置) + List tcPrList = new ArrayList<>(); + RPr templateRPr = null; + for (Object cellObj : existingRow.getContent()) { + if (cellObj instanceof JAXBElement) { + JAXBElement cellElement = (JAXBElement) cellObj; + Tc templateCell = cellElement.getValue(); + TcPr tcPr = templateCell.getTcPr(); + // 设置单元格宽度 + if (tcPr == null) { + tcPr = factory.createTcPr(); + } + TblWidth cellWidth = factory.createTblWidth(); + cellWidth.setType("dxa"); + cellWidth.setW(BigInteger.valueOf(5000 / tableKeys.size())); + tcPr.setTcW(cellWidth); + tcPrList.add(tcPr); + // 从第一个单元格获取字体样式 + if (templateRPr == null && !templateCell.getContent().isEmpty() && templateCell.getContent().get(0) instanceof P) { + P templateP = (P) templateCell.getContent().get(0); + templateRPr = Docx4jUtil.getTcPrFromParagraph(templateP); + } + } + } tbl.getContent().remove(existingRow); // 迭代增加行,需要填充的表格keys在tableKeys集合中 for (Map stringStringMap : dataList) { - Tr newRow = Docx4jUtil.createCustomRow(factory, stringStringMap, tableKeys, trPr, tcPr, true); + Tr newRow = Docx4jUtil.createCustomRow(factory, stringStringMap, tableKeys, trPr, tcPrList, templateRPr, true); tbl.getContent().add(newRow); } } else { diff --git a/detection/src/main/java/com/njcn/gather/report/utils/BookmarkUtil.java b/detection/src/main/java/com/njcn/gather/report/utils/BookmarkUtil.java index 1cd1018c..1bd407ff 100644 --- a/detection/src/main/java/com/njcn/gather/report/utils/BookmarkUtil.java +++ b/detection/src/main/java/com/njcn/gather/report/utils/BookmarkUtil.java @@ -93,18 +93,19 @@ public class BookmarkUtil { idx = idx + 1; parentContent.add(idx, p); } - else if (textFromP.startsWith(PowerIndexEnum.IMBV.getDesc()) - || textFromP.startsWith(PowerIndexEnum.HV.getDesc()) - || textFromP.startsWith(PowerIndexEnum.HI.getDesc()) - - ) { - // 另起一页 - P pagePara = Docx4jUtil.getPageBreak(); - idx = idx + 1; - parentContent.add(idx, pagePara); - idx = idx + 1; - parentContent.add(idx, element); - }else if(textFromP.startsWith("注:基波电流幅值5.000A,基波频率50.0Hz,各次间谐波电流含有率均为3.0%。")){ +// else if (textFromP.startsWith(PowerIndexEnum.IMBV.getDesc()) +// || textFromP.startsWith(PowerIndexEnum.HV.getDesc()) +// || textFromP.startsWith(PowerIndexEnum.HI.getDesc()) +// +// ) { +// // 另起一页 +// P pagePara = Docx4jUtil.getPageBreak(); +// idx = idx + 1; +// parentContent.add(idx, pagePara); +// idx = idx + 1; +// parentContent.add(idx, element); +// } + else if(textFromP.startsWith("注:基波电流幅值5.000A,基波频率50.0Hz,各次间谐波电流含有率均为3.0%。")){ idx = idx + 1; parentContent.add(idx, element); P pagePara = Docx4jUtil.getPageBreak(); diff --git a/detection/src/main/java/com/njcn/gather/report/utils/Docx4jUtil.java b/detection/src/main/java/com/njcn/gather/report/utils/Docx4jUtil.java index e1f49020..68a3de3c 100644 --- a/detection/src/main/java/com/njcn/gather/report/utils/Docx4jUtil.java +++ b/detection/src/main/java/com/njcn/gather/report/utils/Docx4jUtil.java @@ -322,17 +322,20 @@ public class Docx4jUtil { } /** - * 根据已知信息创建新航 + * 根据已知信息创建新行 * - * @param factory 工厂 - * @param valueMap 数据 - * @param tableKeys keys - * @param trPr 行样式 - * @param tcPr 单元格样式 + * @param factory 工厂 + * @param valueMap 数据 + * @param tableKeys keys + * @param trPr 行样式 + * @param tcPrList 每个单元格的样式列表(用于保留各单元格独立的边框设置) + * @param templateRPr 模板中的字体样式(可为null,为null时使用默认宋体10号) + * @param centerFlag 是否居中 */ - public static Tr createCustomRow(ObjectFactory factory, Map valueMap, List tableKeys, TrPr trPr, TcPr tcPr, boolean centerFlag) { + public static Tr createCustomRow(ObjectFactory factory, Map valueMap, List tableKeys, TrPr trPr, List tcPrList, RPr templateRPr, boolean centerFlag) { Tr row = factory.createTr(); - for (String tableKey : tableKeys) { + for (int i = 0; i < tableKeys.size(); i++) { + String tableKey = tableKeys.get(i); Tc cell = factory.createTc(); P paragraph = factory.createP(); R run = factory.createR(); @@ -341,20 +344,41 @@ public class Docx4jUtil { text.setValue(value); run.getContent().add(text); paragraph.getContent().add(run); - // 字体 - // 设置字体 + + // 从模板复制字体样式 RPr rPr = factory.createRPr(); - RFonts rFonts = factory.createRFonts(); - if (containsChinese(value)) { + if (templateRPr != null) { + // 复制字体 + if (templateRPr.getRFonts() != null) { + RFonts rFonts = factory.createRFonts(); + rFonts.setEastAsia(templateRPr.getRFonts().getEastAsia()); + rFonts.setAscii(templateRPr.getRFonts().getAscii()); + rFonts.setHAnsi(templateRPr.getRFonts().getHAnsi()); + rPr.setRFonts(rFonts); + } + // 复制字号 + if (templateRPr.getSz() != null) { + HpsMeasure sz = factory.createHpsMeasure(); + sz.setVal(templateRPr.getSz().getVal()); + rPr.setSz(sz); + } + if (templateRPr.getSzCs() != null) { + HpsMeasure szCs = factory.createHpsMeasure(); + szCs.setVal(templateRPr.getSzCs().getVal()); + rPr.setSzCs(szCs); + } + } else { + // 默认使用宋体10号 + RFonts rFonts = factory.createRFonts(); rFonts.setEastAsia("宋体"); rFonts.setAscii("宋体"); rFonts.setHAnsi("宋体"); - } else { - rFonts.setEastAsia("Arial"); - rFonts.setAscii("Arial"); - rFonts.setHAnsi("Arial"); + rPr.setRFonts(rFonts); + HpsMeasure sz = factory.createHpsMeasure(); + sz.setVal(new BigInteger("20")); + rPr.setSz(sz); } - rPr.setRFonts(rFonts); + // 设置段落居中 if (centerFlag) { PPr pPr = factory.createPPr(); @@ -363,20 +387,19 @@ public class Docx4jUtil { pPr.setJc(jc); paragraph.setPPr(pPr); } - if (value.equals("不合格")) { + // 不合格标红 + if (value != null && value.equals("不合格")) { Color color = factory.createColor(); - // 红色 color.setVal("FF0000"); rPr.setColor(color); - run.setRPr(rPr); } - HpsMeasure sz = factory.createHpsMeasure(); - // 10号字体 = 20 half-points - sz.setVal(new BigInteger("20")); - rPr.setSz(sz); + run.setRPr(rPr); cell.getContent().add(paragraph); - cell.setTcPr(tcPr); + // 使用对应位置的单元格样式(保留边框设置) + if (tcPrList != null && i < tcPrList.size()) { + cell.setTcPr(tcPrList.get(i)); + } row.getContent().add(cell); row.setTrPr(trPr); }