欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java 使用POI合并两个word文档

程序员文章站 2022-07-06 16:31:10
java poi合并两个word文档有需要的可以将主函数中写死的地方改为一个listimport java.io.fileinputstream;import java.io.fileoutputst...

java poi合并两个word文档

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

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 合并

 //两个对象进行追加
    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;
  }

无限合并

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

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

相关标签: java POI word