使用poi生成的多个word文档,在对生成的这多个文档进行合并时报错。求解围。

小虾
  • 1
新手上路,请多包涵

报错信息
目前的猜想为word转为xml,但是无法正常解析,导致报错。。不知道怎么解决。。(word文档通过poi已成功生成)

合并代码:
XWPFDocument src1Document = document;

    XWPFParagraph p = src1Document.createParagraph();
    //设置分页符
    p.setPageBreak(true);
    CTBody src1Body = src1Document.getDocument().getBody();
    XWPFDocument src2Document = doucDocument2;
    CTBody src2Body = src2Document.getDocument().getBody();
    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("<"));
    System.out.println(prefix);
    System.out.println(mainPart);
    System.out.println(addPart);
    System.out.println(sufix);
    CTBody makeBody = CTBody.Factory.parse(prefix + mainPart + addPart + sufix); //此处不能解析
    src1Body.set(makeBody);
    return src1Document;


导入的依赖:

<dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-excelant</artifactId>
        <version>3.10-FINAL</version>
    </dependency>
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-scratchpad</artifactId>
        <version>3.17</version>
    </dependency>

    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml</artifactId>
        <version>3.17</version>
    </dependency>

    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml-schemas</artifactId>
        <version>3.17</version>
    </dependency>

    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>ooxml-schemas</artifactId>
        <version>1.3</version>
    </dependency>

    <dependency>
        <groupId>org.apache.xmlbeans</groupId>
        <artifactId>xmlbeans</artifactId>
        <version>3.1.0</version>
    </dependency>


回复
阅读 648
1 个回答

试试用spire.doc for java来合并word文档,可以承接前一个文档的最后一段合并,也可以从新的一页开始合并

参考教程:Java 合并Word文档

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
宣传栏