河北报告定制化改动

This commit is contained in:
2025-12-02 13:52:07 +08:00
parent 9ab5d42439
commit 9f11f7ec11
2 changed files with 113 additions and 8 deletions

View File

@@ -1600,18 +1600,37 @@ 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

@@ -436,7 +436,93 @@ public class Docx4jUtil {
}
/**
* 根据已知信息创建新
* 根据已知信息创建新
*
* @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<String, String> valueMap, List<String> tableKeys, TrPr trPr, List<TcPr> tcPrList, RPr templateRPr, boolean centerFlag) {
Tr row = factory.createTr();
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();
String value = valueMap.get(tableKey);
Text text = factory.createText();
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();
rFonts.setEastAsia("宋体");
rFonts.setAscii("宋体");
rFonts.setHAnsi("宋体");
rPr.setRFonts(rFonts);
HpsMeasure sz = factory.createHpsMeasure();
sz.setVal(new BigInteger("20"));
rPr.setSz(sz);
}
// 设置段落居中
if (centerFlag) {
PPr pPr = factory.createPPr();
Jc jc = factory.createJc();
jc.setVal(JcEnumeration.CENTER);
pPr.setJc(jc);
paragraph.setPPr(pPr);
}
// 不合格标红
if (value != null && value.equals("不合格")) {
Color color = factory.createColor();
color.setVal("FF0000");
rPr.setColor(color);
}
run.setRPr(rPr);
cell.getContent().add(paragraph);
// 使用对应位置的单元格样式(保留边框设置)
if (tcPrList != null && i < tcPrList.size()) {
cell.setTcPr(tcPrList.get(i));
}
row.getContent().add(cell);
row.setTrPr(trPr);
}
return row;
}
/**
* 根据已知信息创建新行
*
* @param factory 工厂
* @param valueMap 数据