Java 通过POI填充模版word [word工具类]
< ! -- poi -->
< dependency>
< groupId> org.apache.poi< /groupId>
< artifactId> poi< /artifactId>
< version> 3.1 7 < /version>
< /dependency>
< dependency>
< groupId> org.apache.poi< /groupId>
< artifactId> poi-ooxml< /artifactId>
< version> 3.1 7 < /version>
< /dependency>
< dependency>
< groupId> org.apache.poi< /groupId>
< artifactId> poi-ooxml-schemas< /artifactId>
< version> 3.1 7 < /version>
< /dependency>
public class WordUtils {
public static void compile ( String path, String outPath, Map < String , Object > dict) throws Exception {
FileInputStream is = new FileInputStream ( path) ;
XWPFDocument document = new XWPFDocument ( is) ;
if ( dict != null ) {
WordUtils . compileText ( document, dict) ;
WordUtils . compileTable ( document, dict) ;
}
File f = new File ( outPath. substring ( 0 , outPath. lastIndexOf ( File . separator) ) ) ;
if ( ! f. exists ( ) ) {
f. mkdirs ( ) ;
}
FileOutputStream out = new FileOutputStream ( outPath) ;
document. write ( out) ;
}
public static void compileText ( XWPFDocument document, Map < String , Object > dict) {
Iterator < XWPFParagraph > iterator = document. getParagraphsIterator ( ) ;
XWPFParagraph paragraph = null ;
while ( iterator. hasNext ( ) ) {
paragraph = iterator. next ( ) ;
if ( checkText ( paragraph. getText ( ) ) ) {
replaceValue ( paragraph, dict) ;
}
}
}
public static void compileTable ( XWPFDocument document, Map < String , Object > dict) {
Iterator < XWPFTable > tableList = document. getTablesIterator ( ) ;
XWPFTable table;
List < XWPFTableRow > rows;
List < XWPFTableCell > cells;
while ( tableList. hasNext ( ) ) {
table = tableList. next ( ) ;
if ( checkText ( table. getText ( ) ) ) {
rows = table. getRows ( ) ;
for ( XWPFTableRow row : rows) {
cells = row. getTableCells ( ) ;
for ( XWPFTableCell cell : cells) {
if ( checkText ( cell. getText ( ) ) ) {
List < XWPFParagraph > paragraphs = cell. getParagraphs ( ) ;
for ( XWPFParagraph paragraph : paragraphs) {
replaceValue ( paragraph, dict) ;
}
}
}
}
}
}
}
private static void replaceValue ( XWPFParagraph paragraph, Map < String , Object > dict) {
String nextLine;
List < XWPFRun > runs = paragraph. getRuns ( ) ;
for ( int i = 0 ; i < runs. size ( ) ; i++ ) {
String readLine = runs. get ( i) . text ( ) ;
if ( StringUtils . isEmpty ( readLine) || ! readLine. contains ( "$" ) ) continue ;
StringBuffer sb = new StringBuffer ( ) ;
while ( readLine. contains ( "$" ) ) {
int left;
if ( readLine. contains ( "${" ) ) {
left = readLine. indexOf ( "${" ) ;
} else {
if ( runs. size ( ) < i+ 1 ) {
break ;
}
nextLine = runs. get ( i+ 1 ) . text ( ) ;
if ( ! nextLine. startsWith ( "{" ) ) break ;
readLine += nextLine;
paragraph. removeRun ( i+ 1 ) ;
left = readLine. indexOf ( "${" ) ;
}
sb. append ( readLine. substring ( 0 , left) ) ;
while ( runs. size ( ) >= i+ 1 && ! readLine. contains ( "}" ) ) {
nextLine = runs. get ( i+ 1 ) . text ( ) ;
readLine += nextLine;
paragraph. removeRun ( i+ 1 ) ;
}
int right = readLine. indexOf ( "}" ) ;
if ( right == - 1 ) break ;
sb. append ( dict. getOrDefault ( readLine. substring ( left, right+ 1 ) , "" ) ) ;
if ( right + 1 < readLine. length ( ) ) {
sb. append ( readLine. substring ( right + 1 ) ) ;
}
readLine = sb. toString ( ) ;
}
runs. get ( i) . setText ( sb. toString ( ) , 0 ) ;
}
}
private static boolean checkText ( String text) {
return text. contains ( "$" ) ;
}
}
@Test
public void TestReplaceWord ( ) throws Exception {
Map < String , Object > strData = new HashMap < > ( ) ;
strData. put ( "${title}" , "测试文档标题" ) ;
strData. put ( "${name}" , "小陈" ) ;
strData. put ( "${company}" , "大刘的魔法学院" ) ;
strData. put ( "${leader}" , "张" ) ;
String readPath = "C:\\Users\\Lenovo\\Desktop\\DOCX_demo.docx" ;
String outPath = "C:\\Users\\Lenovo\\Desktop\\ABC\\EFG\\Demo.docx" ;
WordUtils . compile ( readPath, outPath, strData) ;
}