|
@@ -803,10 +803,12 @@ public class ZhcxCommissionCheckServiceImpl extends ServiceImpl<ZhcxCommissionCh
|
|
|
@Override
|
|
|
public R exportFile(Map<String, Object> map, HttpServletResponse response) throws Exception {
|
|
|
// 前端传来模板id
|
|
|
- String fileId = "d1b3271ce2ec45c2915f2dbcd819f45a";
|
|
|
+ String fileId = "";
|
|
|
Object fileIdObj = map.get("fileId");
|
|
|
if (Objects.nonNull(fileIdObj)) {
|
|
|
- //fileId = (String) fileIdObj;
|
|
|
+ fileId = (String) fileIdObj;
|
|
|
+ } else {
|
|
|
+ fileId = "7a3213f88d7146e88f1508b61dcb3202";
|
|
|
}
|
|
|
Object projectObj = map.get("projectId");
|
|
|
Object machineNoObj = map.get("machineNo");
|
|
@@ -857,14 +859,14 @@ public class ZhcxCommissionCheckServiceImpl extends ServiceImpl<ZhcxCommissionCh
|
|
|
//移动到页眉
|
|
|
builder.moveToHeaderFooter(HeaderFooterType.HEADER_PRIMARY); // 主页眉
|
|
|
// 如果有多个页眉,可以选择HeaderFooterType.HEADER_FIRST, HeaderFooterType.HEADER_EVEN等
|
|
|
- for (com.aspose.words.Field field : doc.getRange().getFields()) {
|
|
|
- if ("«projectName»".equals(field.getResult())) { // 替换FieldName为你的字段名称
|
|
|
- // 在这里操作field对象
|
|
|
- builder.moveToField(field, true);
|
|
|
- builder.write(projectManageDo.getProjectName());
|
|
|
- field.setResult("");
|
|
|
- }
|
|
|
- }
|
|
|
+// for (com.aspose.words.Field field : doc.getRange().getFields()) {
|
|
|
+// if ("«projectName»".equals(field.getResult())) { // 替换FieldName为你的字段名称
|
|
|
+// // 在这里操作field对象
|
|
|
+// builder.moveToField(field, true);
|
|
|
+// builder.write(projectManageDo.getProjectName());
|
|
|
+// field.setResult("");
|
|
|
+// }
|
|
|
+// }
|
|
|
List<JSONObject> resultType = zhcxCommissionCheckDetailDao.getResultDictList();
|
|
|
if (list.isEmpty() || checkTreeList.isEmpty()) {
|
|
|
return R.error("该检查内容没有数据");
|
|
@@ -1071,9 +1073,58 @@ public class ZhcxCommissionCheckServiceImpl extends ServiceImpl<ZhcxCommissionCh
|
|
|
// 创建模板A的副本
|
|
|
Document cloneDocA = indexDoc[0].deepClone();
|
|
|
//Document cloneDocA = splitDocs[0].deepClone();
|
|
|
+ // 在合并前插入一个分节符
|
|
|
+ cloneDocA.getLastSection().getBody().appendChild(new Paragraph(cloneDocA));
|
|
|
+ cloneDocA.getLastSection().getBody().getLastParagraph().appendChild(new Run(cloneDocA, "\f")); // 分节符
|
|
|
// 在副本上合并文档B
|
|
|
cloneDocA.appendDocument(doc, ImportFormatMode.KEEP_SOURCE_FORMATTING);
|
|
|
- //cloneDocA.appendDocument(doc, ImportFormatMode.KEEP_SOURCE_FORMATTING);
|
|
|
+ //刷新目录
|
|
|
+ refreshTableOfContents(cloneDocA);
|
|
|
+ // 获取第一个文档的总页数
|
|
|
+ int firstDocPageCount = cloneDocA.getPageCount() - doc.getPageCount();
|
|
|
+ // 获取第二部分的第一节
|
|
|
+ Section sectionPart2 = cloneDocA.getSections().get(cloneDocA.getSections().getCount() - 1);
|
|
|
+ // 设置第二部分的页码起始值为第一部分的页数加 1
|
|
|
+ sectionPart2.getPageSetup().setRestartPageNumbering(true);
|
|
|
+ sectionPart2.getPageSetup().setPageStartingNumber(firstDocPageCount + 1);
|
|
|
+
|
|
|
+ // 获取文档的第一节
|
|
|
+ Section firstSection = cloneDocA.getSections().get(0);
|
|
|
+ // 获取第一页的页眉
|
|
|
+ HeaderFooter firstPageHeader = firstSection.getHeadersFooters().getByHeaderFooterType(HeaderFooterType.HEADER_FIRST);
|
|
|
+ // 获取第一页的页脚
|
|
|
+ HeaderFooter firstPageFooter = firstSection.getHeadersFooters().getByHeaderFooterType(HeaderFooterType.FOOTER_FIRST);
|
|
|
+ // 如果第一页没有单独的页眉,则使用主页眉
|
|
|
+ if (firstPageHeader == null) {
|
|
|
+ firstPageHeader = firstSection.getHeadersFooters().getByHeaderFooterType(HeaderFooterType.HEADER_PRIMARY);
|
|
|
+ firstPageFooter = firstSection.getHeadersFooters().getByHeaderFooterType(HeaderFooterType.FOOTER_PRIMARY);
|
|
|
+ }
|
|
|
+ // 遍历文档的所有节(从第二个节开始)
|
|
|
+ for (int i = 1; i < cloneDocA.getSections().getCount(); i++) {
|
|
|
+ Section currentSection = cloneDocA.getSections().get(i);
|
|
|
+ // 获取当前节的主页眉
|
|
|
+ HeaderFooter currentHeader = currentSection.getHeadersFooters().getByHeaderFooterType(HeaderFooterType.HEADER_PRIMARY);
|
|
|
+ // 获取当前节的页脚
|
|
|
+ HeaderFooter footer = currentSection.getHeadersFooters().getByHeaderFooterType(HeaderFooterType.FOOTER_PRIMARY);
|
|
|
+ // 清除当前页眉的内容
|
|
|
+ currentHeader.removeAllChildren();
|
|
|
+ footer.removeAllChildren();
|
|
|
+ // 将第一页页眉的内容克隆到当前页眉
|
|
|
+ for (Node node : firstPageHeader.getChildNodes(NodeType.ANY, true).toArray()) {
|
|
|
+ Node clonedNode = node.deepClone(true);
|
|
|
+ currentHeader.appendChild(clonedNode);
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ // 复制第一页页脚的内容到当前页脚
|
|
|
+ for (Node node : firstPageFooter.getChildNodes(NodeType.ANY, true).toArray()) {
|
|
|
+ Node clonedNode = node.deepClone(true);
|
|
|
+ footer.appendChild(clonedNode);
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //刷新目录
|
|
|
+ refreshTableOfContents(cloneDocA);
|
|
|
+
|
|
|
if ("export".equals(exportType)) {
|
|
|
exportWord(cloneDocA, response);
|
|
|
} else {
|
|
@@ -1083,6 +1134,24 @@ public class ZhcxCommissionCheckServiceImpl extends ServiceImpl<ZhcxCommissionCh
|
|
|
return R.ok();
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 刷新文档中的所有目录
|
|
|
+ */
|
|
|
+ private static void refreshTableOfContents(Document doc) throws Exception {
|
|
|
+ // 遍历文档中的所有字段
|
|
|
+ for (com.aspose.words.Field field : doc.getRange().getFields()) {
|
|
|
+ // 检查字段是否为目录字段(TOC)
|
|
|
+ if (field.getType() == FieldType.FIELD_TOC) {
|
|
|
+ // 更新目录字段
|
|
|
+ field.update();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 如果需要更新整个文档的布局(例如页码变化)
|
|
|
+ doc.updateFields();
|
|
|
+ doc.updatePageLayout();
|
|
|
+ }
|
|
|
+
|
|
|
private int sort1(List<String> parts1, List<String> parts2) {
|
|
|
int length1 = parts1.size();
|
|
|
int length2 = parts2.size();
|