如何合并多个word 里面的内容

public  void  wordMix(String  src1Input,String  src2Input,String  src3Input) throws Exception {
            InputStream in1 = null;
         InputStream in2 = null;
        OPCPackage src1Package = null;
        OPCPackage src2Package = null;
           OutputStream dest = new FileOutputStream(src3Input);
           try {
               in1 = new FileInputStream(src1Input);
               in2 = new FileInputStream(src2Input);
               src1Package = OPCPackage.open(in1);
               src2Package = OPCPackage.open(in2);
           } catch (Exception e) {
               e.printStackTrace();
           }
    
         //  @SuppressWarnings("resource")
      XWPFDocument src1Document = new XWPFDocument(src1Package);
           CTBody src1Body = src1Document.getDocument().getBody();
           XWPFParagraph p = src1Document.createParagraph();
           //设置分页符
          // p.setPageBreak(true);
          // @SuppressWarnings("resource")
      XWPFDocument src2Document = new XWPFDocument(src2Package);
           CTBody src2Body = src2Document.getDocument().getBody();
           appendBody(src1Body, src2Body);
           src1Document.write(dest);
    
    }
public  void  wordMixMOM(String  src1Input,String  src2Input,String  src3Input) throws Exception {
   InputStream in1 = null;
   InputStream in2 = null;
   OPCPackage src1Package = null;
   OPCPackage src2Package = null;
   OutputStream dest = new FileOutputStream(src3Input);
   try {
      in1 = new FileInputStream(src1Input);
      in2 = new FileInputStream(src2Input);
      src1Package = OPCPackage.open(in1);
      src2Package = OPCPackage.open(in2);
   } catch (Exception e) {
      e.printStackTrace();
   }
   
   //  @SuppressWarnings("resource")
   XWPFDocument src1Document = new XWPFDocument(src1Package);
   CTBody src1Body = src1Document.getDocument().getBody();
   XWPFParagraph p = src1Document.createParagraph();
   //设置分页符
   //p.setPageBreak(true);
   // @SuppressWarnings("resource")
   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);
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值