gpt4 book ai didi

java - 如何使用 Apache POI API 合并 .doc 文件

转载 作者:行者123 更新时间:2023-12-01 13:14:42 25 4
gpt4 key购买 nike

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

        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();
}

我收到此错误的问题:

Your document seemed to be mostly unicode, but the section definition was in bytes! Trying anyway, but things may well go wrong!
Your document seemed to be mostly unicode, but the section definition was in bytes! Trying anyway, but things may well go wrong!
Your document seemed to be mostly unicode, but the section definition was in bytes! Trying anyway, but things may well go wrong!

以下是不同文件的内容:

fich1.doc:大家好

fich2.doc:祝大家一切顺利

fich3.doc:大家好

resullt.doc:大家好B

最佳答案

此错误已在 poi 3.9 jar 中修复:使用过的 jar

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

关于java - 如何使用 Apache POI API 合并 .doc 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22564664/

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