如何使用 Apache POI API 合并 .doc 文件 [英] How to merge .doc files using Apache POI API

查看:40
本文介绍了如何使用 Apache POI API 合并 .doc 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试合并 .doc 文件,我使用了在之前的 stackOverflow 帖子中找到的这段代码:

i'm trying to merge .doc files , i used this code found in previous stackOverflow post :

        public static void main (String[] args) throws Exception {  
        // POI apparently can't create a document from scratch,  
        // so we need an existing empty dummy document  
        HWPFDocument doc = new HWPFDocument(new FileInputStream("fich1.doc"));  
        Range range = doc.getRange();  


        //I can get the entire Document and insert it in the tmp.doc  
        //However any formatting in my word document is lost.  
        HWPFDocument doc2 = new HWPFDocument(new FileInputStream("fich2.doc"));  
        Range range2 = doc2.getRange();  
        range.insertAfter(range2.text());  

        //I can get the information (text only) for each character run/paragraph or section.  
        //Again any formatting in my word document is lost.  
        HWPFDocument doc3 = new HWPFDocument(new FileInputStream("fich3.doc"));  
        Range range3 = doc3.getRange();  
        for(int i=0;i<range3.numCharacterRuns();i++){  
            CharacterRun run3 = range3.getCharacterRun(i);  
            range.insertAfter(run3.text());  
        }  

        OutputStream out = new FileOutputStream("result.doc");  
        doc.write(out);  
        out.flush();  
        out.close();  
}  

我收到此错误的问题:

您的文档似乎主要是 unicode,但节定义是以字节为单位的!无论如何都在尝试,但事情很可能会出错!您的文档似乎主要是 unicode,但部分定义以字节为单位!无论如何都在尝试,但事情很可能会出错!您的文档似乎主要是 unicode,但部分定义以字节为单位!无论如何都在尝试,但很可能会出错!

以下是不同文件的内容:

Here's the content of the different files :

fich1.doc : 大家好

fich2.doc : Bonjour à tous

fich2.doc : Bonjour à tous

fich3.doc : 大家好

result.doc : 大家好

推荐答案

这个 bug 在 poi 3.9 jars 中已修复:used jars

This bug is fixed in poi 3.9 jars : used jars

poi-3.9.jar/poi-ooxml-3.9.jar/poi-ooxml-schemas-3.9.jar/poi-scratchpad-3.9.jar

poi-3.9.jar / poi-ooxml-3.9.jar / poi-ooxml-schemas-3.9.jar / poi-scratchpad-3.9.jar

这篇关于如何使用 Apache POI API 合并 .doc 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆