河北报告定制化改动

This commit is contained in:
2025-12-02 13:37:35 +08:00
parent f4df52dd1c
commit 2012221b73
3 changed files with 86 additions and 44 deletions

View File

@@ -1232,16 +1232,34 @@ public class PqReportServiceImpl extends ServiceImpl<PqReportMapper, PqReport> i
Tr existingRow = (Tr) tbl.getContent().get(rows.size() - 1);
// 获取现有样式
TrPr trPr = existingRow.getTrPr();
JAXBElement<Tc> element = (JAXBElement<Tc>) existingRow.getContent().get(0);
TcPr tcPr = element.getValue().getTcPr();
// 获取每个单元格的TcPr保留各单元格独立的边框设置
List<TcPr> tcPrList = new ArrayList<>();
RPr templateRPr = null;
for (Object cellObj : existingRow.getContent()) {
if (cellObj instanceof JAXBElement) {
JAXBElement<Tc> cellElement = (JAXBElement<Tc>) 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<String, String> 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 {

View File

@@ -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();

View File

@@ -322,17 +322,20 @@ public class Docx4jUtil {
}
/**
* 根据已知信息创建新
* 根据已知信息创建新
*
* @param factory 工厂
* @param valueMap 数据
* @param tableKeys keys
* @param trPr 行样式
* @param tcPr 单元格样式
* @param tcPrList 每个单元格样式列表(用于保留各单元格独立的边框设置)
* @param templateRPr 模板中的字体样式可为null为null时使用默认宋体10号
* @param centerFlag 是否居中
*/
public static Tr createCustomRow(ObjectFactory factory, Map<String, String> valueMap, List<String> tableKeys, TrPr trPr, TcPr tcPr, boolean centerFlag) {
public static Tr createCustomRow(ObjectFactory factory, Map<String, String> valueMap, List<String> tableKeys, TrPr trPr, List<TcPr> 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();
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();
if (containsChinese(value)) {
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);
}
// 设置段落居中
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);
}