5 changed files with 133 additions and 3 deletions
-
6pom.xml
-
116src/main/java/com/heai/modules/base/utils/wordTest.java
-
9src/main/java/com/heai/modules/production/entity/ShopOrderRoutingData.java
-
2src/main/java/com/heai/modules/production/service/impl/DailyPlanServiceImpl.java
-
3src/main/resources/mapper/production/DailyPlanMapper.xml
@ -0,0 +1,116 @@ |
|||
package com.heai.modules.base.utils; |
|||
|
|||
import org.docx4j.openpackaging.packages.WordprocessingMLPackage; |
|||
import org.docx4j.wml.*; |
|||
|
|||
import java.util.List; |
|||
import java.util.Map; |
|||
import java.util.HashMap; |
|||
|
|||
public class wordTest { |
|||
// 替换Word文档中的占位符为实际值 |
|||
public static void replacePlaceholder(WordprocessingMLPackage wordMLPackage, Map<String, String> replacements) { |
|||
// // 遍历文档中的所有段落 |
|||
// List<Object> content = wordMLPackage.getMainDocumentPart().getContent(); |
|||
// for (Object obj : content) { |
|||
// // 处理段落 |
|||
// if (obj instanceof P) { |
|||
// P paragraph = (P) obj; |
|||
// replaceTextInParagraph(paragraph, replacements); |
|||
// } |
|||
// // 处理表格 |
|||
// else if (obj instanceof Tbl) { |
|||
// Tbl table = (Tbl) obj; |
|||
// replaceTextInTable(table, replacements); |
|||
// } |
|||
// } |
|||
|
|||
|
|||
for (Object obj : wordMLPackage.getMainDocumentPart().getContent()) { |
|||
if (obj instanceof P) { |
|||
for (Object p : ((P) obj).getContent()) { |
|||
if (p instanceof Text) { |
|||
Text text = (Text) p; |
|||
String currentText = text.getValue(); |
|||
// 替换占位符 |
|||
for (Map.Entry<String, String> entry : replacements.entrySet()) { |
|||
String placeholder = entry.getKey(); |
|||
String replacement = entry.getValue(); |
|||
if (currentText.contains(placeholder)) { |
|||
currentText = currentText.replace(placeholder, replacement); |
|||
text.setValue(currentText); // 更新文本内容 |
|||
} |
|||
} |
|||
} |
|||
} |
|||
} |
|||
} |
|||
} |
|||
|
|||
// 处理段落中的文本替换 |
|||
private static void replaceTextInParagraph(P paragraph, Map<String, String> replacements) { |
|||
for (Object content : paragraph.getContent()) { |
|||
if (content instanceof R) { |
|||
R run = (R) content; |
|||
for (Object runContent : run.getContent()) { |
|||
if (runContent instanceof Text) { |
|||
Text textElement = (Text) runContent; |
|||
String currentText = textElement.getValue(); |
|||
System.out.println("Before replacement: " + currentText); |
|||
// 替换占位符 |
|||
for (Map.Entry<String, String> entry : replacements.entrySet()) { |
|||
String placeholder = entry.getKey(); |
|||
String replacement = entry.getValue(); |
|||
if (currentText.contains(placeholder)) { |
|||
currentText = currentText.replace(placeholder, replacement); |
|||
textElement.setValue(currentText); // 更新文本内容 |
|||
System.out.println("After replacement: " + currentText); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
} |
|||
} |
|||
} |
|||
|
|||
// 处理表格中的文本替换 |
|||
private static void replaceTextInTable(Tbl table, Map<String, String> replacements) { |
|||
for (Object tableRow : table.getContent()) { |
|||
if (tableRow instanceof Tr) { |
|||
Tr row = (Tr) tableRow; |
|||
for (Object cell : row.getContent()) { |
|||
if (cell instanceof Tc) { |
|||
Tc tableCell = (Tc) cell; |
|||
for (Object cellContent : tableCell.getContent()) { |
|||
if (cellContent instanceof P) { |
|||
P cellParagraph = (P) cellContent; |
|||
replaceTextInParagraph(cellParagraph, replacements); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
} |
|||
} |
|||
} |
|||
|
|||
|
|||
public static void main(String[] args) throws Exception { |
|||
// 加载模板文档 |
|||
WordprocessingMLPackage template = WordprocessingMLPackage.load(new java.io.File("D:\\java\\模板.docx")); |
|||
|
|||
// 定义要插入到模板中的数据 |
|||
Map<String, String> data = new HashMap<>(); |
|||
data.put("{角色名}", "杨壮壮"); |
|||
data.put("{等级}", "99"); |
|||
data.put("{职业}", "散修"); |
|||
data.put("{特性}", "老当益壮"); |
|||
data.put("{境界}", "结丹期"); |
|||
data.put("{称号}", "CKP之王"); |
|||
data.put("{必杀技}", "老汉推车"); |
|||
// 执行替换操作 |
|||
replacePlaceholder(template, data); |
|||
|
|||
// 将结果保存到新文件 |
|||
template.save(new java.io.File("D:\\java\\输出结果.docx")); |
|||
} |
|||
} |
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue