合并效果:
1. 引入maven依赖
< dependencies>
< dependency>
< groupId> org.springframework.boot</ groupId>
< artifactId> spring-boot-starter-web</ artifactId>
< version> RELEASE</ version>
</ dependency>
< dependency>
< groupId> org.apache.poi</ groupId>
< artifactId> poi</ artifactId>
< version> 3.7</ version>
</ dependency>
< dependency>
< groupId> org.apache.poi</ groupId>
< artifactId> poi-ooxml</ artifactId>
< version> 3.7</ version>
</ dependency>
< dependency>
< groupId> org.apache.poi</ groupId>
< artifactId> poi-scratchpad</ artifactId>
< version> 3.7</ version>
</ dependency>
</ dependencies>
2. 实现代码
package com. java. util;
import org. apache. poi. openxml4j. opc. OPCPackage;
import org. apache. poi. xwpf. usermodel. XWPFDocument;
import org. apache. tomcat. util. http. fileupload. IOUtils;
import org. apache. xmlbeans. XmlOptions;
import org. openxmlformats. schemas. wordprocessingml. x2006. main. CTBody;
import java. io. FileNotFoundException;
import java. io. FileOutputStream;
import java. io. IOException;
import java. io. OutputStream;
import java. util. ArrayList;
import java. util. List;
public class MergeDoc {
public static void mergeDoc ( List< String> srcDocxs, String destDocx) {
OutputStream dest = null;
List< OPCPackage> opcpList = new ArrayList < OPCPackage> ( ) ;
int length = null == srcDocxs ? 0 : srcDocxs. size ( ) ;
for ( int i = 0 ; i < length; i++ ) {
String doc = srcDocxs. get ( i) ;
OPCPackage srcPackage = null;
try {
srcPackage = OPCPackage. open ( doc) ;
} catch ( Exception e) {
e. printStackTrace ( ) ;
}
if ( null != srcPackage) {
opcpList. add ( srcPackage) ;
}
}
int opcpSize = opcpList. size ( ) ;
if ( opcpSize > 0 ) {
try {
dest = new FileOutputStream ( destDocx) ;
XWPFDocument src1Document = new XWPFDocument ( opcpList. get ( 0 ) ) ;
CTBody src1Body = src1Document. getDocument ( ) . getBody ( ) ;
if ( opcpSize > 1 ) {
for ( int i = 1 ; i < opcpSize; i++ ) {
OPCPackage src2Package = opcpList. get ( i) ;
XWPFDocument src2Document = new XWPFDocument ( src2Package) ;
CTBody src2Body = src2Document. getDocument ( ) . getBody ( ) ;
appendBody ( src1Body, src2Body) ;
}
}
src1Document. write ( dest) ;
} catch ( FileNotFoundException e) {
e. printStackTrace ( ) ;
} catch ( IOException e) {
e. printStackTrace ( ) ;
} catch ( Exception e) {
e. printStackTrace ( ) ;
} finally {
IOUtils. closeQuietly ( dest) ;
}
}
}
private static void appendBody ( CTBody src, CTBody append) throws Exception {
XmlOptions optionsOuter = new XmlOptions ( ) ;
optionsOuter. setSaveOuter ( ) ;
String appendString = append. xmlText ( optionsOuter) ;
appendString = appendString. replaceAll ( "<w:p><w:pPr><w:spacing w:line=\\\"360\\\" w:lineRule=\\\"auto\\\"/><w:jc w:val=\\\"right\\\"/><w:rPr><w:rFonts w:ascii=\\\"仿宋\\\" w:eastAsia=\\\"仿宋\\\" w:hAnsi=\\\"仿宋\\\"/><w:sz w:val=\\\"24\\\"/></w:rPr></w:pPr></w:p>" , "" ) ;
appendString = appendString. replaceAll ( "<w:p><w:pPr xsi:nil=\\\"true\\\" xmlns:xsi=\\\"xsi\\\"/></w:p>" , "" ) ;
appendString = appendString. replaceAll ( "<w:p><w:pPr><w:ind w:firstLineChars=\\\"200\\\" w:firstLine=\\\"600\\\"/><w:jc w:val=\\\"right\\\"/><w:rPr><w:rFonts w:ascii=\\\"仿宋\\\" w:eastAsia=\\\"仿宋\\\" w:hAnsi=\\\"仿宋\\\"/><w:sz w:val=\\\"30\\\"/><w:szCs w:val=\\\"30\\\"/></w:rPr></w:pPr></w:p>" , "" ) ;
appendString = appendString. replaceAll ( "<w:spacing w:line=\\\"360\\\" w:lineRule=\\\"auto\\\"/>" , "" ) ; ;
String srcString = src. xmlText ( ) ;
srcString = srcString. replaceAll ( "<w:p><w:pPr><w:ind w:firstLineChars=\\\"200\\\" w:firstLine=\\\"600\\\"/><w:jc w:val=\\\"right\\\"/><w:rPr><w:rFonts w:ascii=\\\"仿宋\\\" w:eastAsia=\\\"仿宋\\\" w:hAnsi=\\\"仿宋\\\"/><w:sz w:val=\\\"30\\\"/><w:szCs w:val=\\\"30\\\"/></w:rPr></w:pPr></w:p>" , "" ) ;
srcString = srcString. replaceAll ( "<w:spacing w:line=\\\"360\\\" w:lineRule=\\\"auto\\\"/>" , "" ) ;
srcString = srcString. replaceAll ( "<w:p><w:pPr><w:spacing w:line=\\\"360\\\" w:lineRule=\\\"auto\\\"/><w:jc w:val=\\\"right\\\"/><w:rPr><w:rFonts w:ascii=\\\"仿宋\\\" w:eastAsia=\\\"仿宋\\\" w:hAnsi=\\\"仿宋\\\"/><w:sz w:val=\\\"24\\\"/></w:rPr></w:pPr></w:p>" , "" ) ;
srcString = srcString. replaceAll ( "<w:p><w:pPr xsi:nil=\\\"true\\\" xmlns:xsi=\\\"xsi\\\"/></w:p>" , "" ) ;
String prefix = srcString. substring ( 0 , srcString. indexOf ( ">" ) + 1 ) ;
String mainPart = srcString. substring ( srcString. indexOf ( ">" ) + 1 ,
srcString. lastIndexOf ( "<" ) ) ;
String sufix = srcString. substring ( srcString. lastIndexOf ( "<" ) ) ;
String addPart = appendString. substring ( appendString. indexOf ( ">" ) + 1 ,
appendString. lastIndexOf ( "<" ) ) ;
CTBody makeBody = CTBody. Factory. parse ( prefix + mainPart + addPart
+ sufix) ;
src. set ( makeBody) ;
}
}
3. 测试
public static void main ( String[ ] args) {
List< String> srcs = new ArrayList < String> ( ) ;
srcs. add ( "C:\\input1.docx" ) ;
srcs. add ( "C:\\input2.docx" ) ;
mergeDoc ( srcs, "C:\\output.docx" ) ;
}