代码 word模板及生成word下载
先制作word模板
比如说这个模板。首先对模板给于占位,我为了方便用代码获取,前面加了$,这都随意,在标号占位后
一定记得 例如$name选中右键复制->粘贴只保留文本(有时候打码读取时会有占位符)
最后记得word另存为docx文件
pom加 jar
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-excelant</artifactId>
<version>4.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>4.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>4.0.0</version>
</dependency>
<!-- 生成图片-->
<dependency>
<groupId>org.jfree</groupId>
<artifactId>jfreechart</artifactId>
<version>1.0.19</version>
</dependency>
<dependency>
<!--支持插入图片-->
<groupId>org.docx4j</groupId>
<artifactId>docx4j</artifactId>
<version>3.3.1</version>
</dependency>
制作好模板后 核心代码
/**
* 将word中某些标签替换成指定的值,并生成一个新的word文档。
*
* @param templateFilePath word模板文件路径
* @param outFilePath 填充后输出文件路径
* @param map key:word中的占位标签,value对应标签要替换的值。
* @throws IOException
*/
public static void insertAndOutFile(String templateFilePath, String outFilePath, Map<String, String> map) throws IOException, InvalidFormatException {
//准备工作,生成docx对象
String templatePath = templateFilePath;
InputStream is = new FileInputStream(templatePath);
XWPFDocument docx = new XWPFDocument(is);
//获取段落
List<XWPFParagraph> paras = docx.getParagraphs();
for (XWPFParagraph para : paras) {
for (XWPFRun run : para.getRuns()) {
//遍历该段里的所有文本
String str = run.toString().trim();
System.out.println(str);
//如果该段文本包含map中的key,则替换为map中的value值。
Set<String> keySet = map.keySet();
for (String key : keySet) {
if (str.trim().equals(key)) {
System.out.println("key" + map.get(key));
//替换该文本0位置的数据。
run.setText(map.get(key), 0);
}
}
}
}
//获取表格
List<XWPFTable> tables = docx.getTables();
//定位到第一个表格
XWPFTable table = tables.get(0);
//遍历该表格所有的行
for (int i = 0; i < table.getRows().size(); i++) {
XWPFTableRow row = table.getRow(i);
//遍历该行所有的列
for (int j = 0; j < row.getTableCells().size(); j++) {
XWPFTableCell cell = row.getTableCells().get(j);
//获取该格子里所有的段
List<XWPFParagraph> paragraphs = cell.getParagraphs();
for (XWPFParagraph p : paragraphs) {
//遍历该格子里的段
List<XWPFRun> runs = p.getRuns();
for (XWPFRun run : runs) {
//遍历该段里的所有文本
String str = run.toString().trim();
System.out.println(str);
//如果该段文本包含map中的key,则替换为map中的value值。
Set<String> keySet = map.keySet();
for (String key : keySet) {
if (str.trim().equals(key)) {
if (key.contains("$img")) {
System.out.println("key" + map.get(key));
if (StrKit.notBlank(map.get(key)) && key.equals(str.trim())) {
//获取本地图片
run.addPicture(new FileInputStream(map.get(key)),
XWPFDocument.PICTURE_TYPE_PNG,
str+".png",
Units.toEMU(300),
Units.toEMU(150)
);
run.setText("", 0);//删除
} else {
run.setText("", 0);
}
}
else {
System.out.println("key" + map.get(key));
//替换该文本0位置的数据。
run.setText(map.get(key), 0);
}
}
}
}
}
}
}
//输出
OutputStream os = new FileOutputStream(outFilePath);
docx.write(os);
is.close();
os.close();
}
public static void main(String[] args) throws Exception {
Map<String, String> map = new HashMap<>();
map.put("$name", "名字");
map.put("$serial_number", "编号");
map.put("$department", "部门");
map.put("$reason_text", "事由");
map.put("$notice_content_text", "内容");
map.put("$requirement_content_text", "要求");
map.put("$img1", "D://1.png");
map.put("$img2", "D://1.png");
map.put("$img3", "D://1.png");
map.put("$img4", "D://1.png");
map.put("$img5", "");
map.put("$submit", "1215456464");
map.put("$submit_date", "2022-01-02");
insertAndOutFile("D:\\test1.docx", "D:\\out.docx", map);
}
生成后word