gpt4 book ai didi

java 使用POI合并两个word文档

转载 作者:qq735679552 更新时间:2022-09-29 22:32:09 38 4
gpt4 key购买 nike

CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.

这篇CFSDN的博客文章java 使用POI合并两个word文档由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.

java POI合并两个word文档

有需要的可以将主函数中写死的地方改为一个List

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
 
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.xmlbeans.XmlOptions;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTBody;
public class MergeDoc {
     public static void main (String[] args) throws Exception {
         InputStream in1 = null ;
         InputStream in2 = null ;
         OPCPackage src1Package = null ;
         OPCPackage src2Package = null ;
 
         OutputStream dest = new FileOutputStream( "dest.docx" );
         try {
             in1 = new FileInputStream( "/Users/liuyahui/test.docx" );
             in2 = new FileInputStream( "/Users/liuyahui/test1.docx" );
             src1Package = OPCPackage.open(in1);
             src2Package = OPCPackage.open(in2);
         } catch (Exception e) {
             e.printStackTrace();
         }
 
         XWPFDocument src1Document = new XWPFDocument(src1Package);
         CTBody src1Body = src1Document.getDocument().getBody();
         XWPFParagraph p = src1Document.createParagraph();
         //设置分页符
         p.setPageBreak( true );
         XWPFDocument src2Document = new XWPFDocument(src2Package);
         CTBody src2Body = src2Document.getDocument().getBody();
         appendBody(src1Body, src2Body);
         src1Document.write(dest);
     }
 
     private static void appendBody(CTBody src, CTBody append) throws Exception {
         XmlOptions optionsOuter = new XmlOptions();
         optionsOuter.setSaveOuter();
         String appendString = append.xmlText(optionsOuter);
         String srcString = src.xmlText();
         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);
     }
}

poi生成word --XWPFDocument 合并

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//两个对象进行追加
     public  XWPFDocument mergeWord(XWPFDocument document,XWPFDocument doucDocument2) throws Exception {
       XWPFDocument src1Document =document ;
       XWPFParagraph p = src1Document.createParagraph();
       //设置分页符
       p.setPageBreak( true );
       CTBody src1Body = src1Document.getDocument().getBody();
       XWPFDocument src2Document = doucDocument2;
       CTBody src2Body = src2Document.getDocument().getBody();
//      XWPFParagraph p2 = src2Document.createParagraph();
       XmlOptions optionsOuter = new XmlOptions();
       optionsOuter.setSaveOuter();
       String appendString = src2Body.xmlText(optionsOuter);
       String srcString = src1Body.xmlText();
       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);
       src1Body.set(makeBody);
       return src1Document;
   }

无限合并

?
1
2
3
4
XWPFDocument xmd=list.get( 0 ); //默认获取第一个作为模板
       for ( int i= 0 ;i<list.size()- 1 ;i++) {
        xmd=mergeWord(xmd,list.get(i+ 1 )); //相继合并
}

以上为个人经验,希望能给大家一个参考,也希望大家多多支持我.

原文链接:https://blog.csdn.net/qq_37043780/article/details/83623847 。

最后此篇关于java 使用POI合并两个word文档的文章就讲到这里了,如果你想了解更多关于java 使用POI合并两个word文档的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

38 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com