word模板制作:
demo使用maven创建项目
pom文件:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>poi_word_test</groupId>
<artifactId>poi_word_test</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>3.17</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.17</version>
</dependency>
</dependencies>
</project>
代码示例:
package com.dml.poiTest.tempWord;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.Bookmarks;
import org.apache.poi.hwpf.usermodel.Range;
import org.apache.poi.xwpf.usermodel.*;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTDocument1;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import java.io.*;
import java.util.*;
public class tempWord {
private static void replaceInWord(Map<String, String> replacements, Map<String, String> marks, XWPFDocument doc, File outfile){
long count1 = 0;
long count2 = 0;
List<XWPFParagraph> paragraphs = doc.getParagraphs();
List<XWPFTable> tables = doc.getTables();
count1 = replaceInParagraphs(replacements, marks, paragraphs, false);
count2 = replaceInTables(replacements, marks, tables);
FileOutputStream fileOutputStream = null;
try {
fileOutputStream = new FileOutputStream(outfile);
doc.write(fileOutputStream);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
fileOutputStream.close();
} catch (IOException e) {