Java Apache POI:从.doc文件读取/写入问题 [英] Java Apache POI : Read / Write from .doc file issue

查看:331
本文介绍了Java Apache POI:从.doc文件读取/写入问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写代码来读取.doc文件作为模板,并在各种迭代之后将数据写入新的.doc文件。我的代码似乎有一些简单的问题,我无法弄清楚。



下面是我写的代码,[我得到基本的骨架只在stackoverflow的某处。 ]

pre $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ b String inputFile =F:\\docx\\input.doc;
String outputFile =F:\\docx\\output.doc;
POIFSFileSystem fs = null;
$ b try {
for(int i = 0; i <3; i ++){
fs = new POIFSFileSystem(new FileInputStream(inputFile));
HWPFDocument doc = new HWPFDocument(fs);
System.out.println(LOOOOOOOOOOOOOOOP - >+ i);
doc = replaceText(doc,$ count,String.valueOf(i));
doc = replaceText(doc,$ filename,FileName+ i);
doc = replaceText(doc,$ inputFile,Input+ i);
doc = replaceText(doc,$ outputFile,Output+ i);
doc = replaceText(doc,$ message,Message+ i);
doc = replaceText(doc,$ snap,Snapshot+ i);
saveWord(outputFile,doc);
}
System.out.println(DONE ...);
}
catch(FileNotFoundException e){
e.printStackTrace();
} catch(IOException e){
e.printStackTrace();


$ b private static HWPFDocument replaceText(HWPFDocument doc,String findText,String replaceText){
Range r1 = doc.getRange();
for(int i = 0; i< r1.numSections(); ++ i){
Section s = r1.getSection(i);
for(int x = 0; x Paragraph p = s.getParagraph(x);
for(int z = 0; z CharacterRun run = p.getCharacterRun(z);
String text = run.text();
if(text.contains(findText)){
run.replaceText(findText,replaceText);
System.out.println(findText:+ findText +replaceText:+ replaceText);
}
}
}
}
return doc;


$ b private static void saveWord(String filePath,HWPFDocument doc)throws FileNotFoundException,IOException {
FileOutputStream out = null;
尝试{
//添加true以使数据追加到输出流中。
out = new FileOutputStream(filePath,true);
doc.write(out);
out.flush();
catch(Exception ex){
ex.printStackTrace();
} finally {
out.close();













这段代码没有任何问题。这里是input.doc的样子,

理论上,它应该包含所有3次迭代的数据,但它只包含第一个数据,然后没有任何数据。它在执行过程中也不会显示任何错误/异常。我也确保outputstream将追加选项作为true。

这是output.doc的样子,


不知道,我在做什么错。



当我运行程序时,我可以在下面的输出中看到,


LOOOOOOOOOOOOOOP ----> 0
findText:$ count replaceText:0
findText:$ filename replaceText:FileName0
findText:$ inputFile replaceText:Input0
findText:$ outputFile replaceText:Output0
findText:$ message replaceText:Message0
findText:$ snap replaceText:Snapshot0
LOOOOOOOOOOOOP ----> 1
findText:$ count replaceText:1
findText:$ filename replaceText:FileName1
findText:$ inputFile replaceText:Input1
findText:$ outputFile replaceText:Output1
findText:$ message replaceText:Message1
findText:$ snap replaceT ext:Snapshot1
LOOOOOOOOOOOOP ----> 2
findText:$ count replaceText:2
findText:$ filename replaceText:FileName2
findText:$ inputFile replaceText:Input2
findText:$ outputFile replaceText:Output2
findText:$ message replaceText:Message2
findText:$ snap replaceText:Snapshot2
DONE ...



正如我在每次迭代中启动输入文件一样新。所以我在迭代期间找到了所有的$元素。这只是他们不会被追加到最终文件中。



有人可以帮忙吗?非常感谢。

解决方案

显然,令人惊讶的是,Apache POI没有任何方法来编写附加到现有的word文档。所以上面的方法是行不通的。

我也尝试了Apache FileUtils,但是它不保留word文档的格式化。我也尝试了docx4j,但只在docx文件中工作,其合并工具类是付费的。

还有另一个框架,Aspose Words,它提供了更好的控制和灵活性。它允许您将内容附加到现有文档,限制为1150个字符。但是,对于我的要求来说,这太多了,因为我的写作不超过设定的限制。所以我用它来实现我想要做的事情。终于成功了。



感谢@D的帮助。 Krauchanka


I am writing a code to read a .doc file as a template and write data in new .doc file after various iterations. My code seems to have some simple issue that I am not able to figure out.

Below is the code I have written, [I got basic skeleton somewhere on stackoverflow only.]

public class HWPFTest {

  public static void main(String[] args) {
        String inputFile = "F:\\docx\\input.doc";
        String outputFile = "F:\\docx\\output.doc";
        POIFSFileSystem fs = null;

        try {
              for (int i = 0; i < 3; i++) {
                    fs = new POIFSFileSystem(new FileInputStream(inputFile));
                    HWPFDocument doc = new HWPFDocument(fs);
                    System.out.println("LOOOOOOOOOOOOP ----> " + i);
                    doc = replaceText(doc, "$count", String.valueOf(i));
                    doc = replaceText(doc, "$filename", "FileName" + i);
                    doc = replaceText(doc, "$inputFile", "Input" + i);
                    doc = replaceText(doc, "$outputFile", "Output" + i);
                    doc = replaceText(doc, "$message", "Message" + i);
                    doc = replaceText(doc, "$snap", "Snapshot" + i);
                    saveWord(outputFile, doc);
              }
              System.out.println("DONE...");
        }
        catch (FileNotFoundException e) {
              e.printStackTrace();
        } catch (IOException e) {
              e.printStackTrace();
        }
  }

  private static HWPFDocument replaceText(HWPFDocument doc, String findText, String replaceText) {
        Range r1 = doc.getRange();
        for (int i = 0; i < r1.numSections(); ++i) {
              Section s = r1.getSection(i);
              for (int x = 0; x < s.numParagraphs(); x++) {
                    Paragraph p = s.getParagraph(x);
                    for (int z = 0; z < p.numCharacterRuns(); z++) {
                          CharacterRun run = p.getCharacterRun(z);
                          String text = run.text();
                          if (text.contains(findText)) {
                               run.replaceText(findText, replaceText);
                               System.out.println("findText: " + findText + " replaceText: " + replaceText);
                          }
                    }
              }
        }
        return doc;
  }


  private static void saveWord(String filePath, HWPFDocument doc) throws FileNotFoundException, IOException {
        FileOutputStream out = null;
        try {
              // Add true to make the data append possible in output stream.
              out = new FileOutputStream(filePath, true);
              doc.write(out);
              out.flush();
        } catch (Exception ex) {
              ex.printStackTrace();
        } finally {
              out.close();
        }
  }

}

The code works without any issues. Here is how the input.doc looks,

After the successful run, the output.doc is also generated. But the issue is that it contains data for only first loop.

Ideaally, it should contain data for all the 3 iterations, but it contains data for only first and then there is nothing. It doesn't show any error / exception during execution as well. I have also made sure that outputstream will have append option as true.

This is how the output.doc looks,

Not sure, what I am doing wrong.

When I run the program, I can see in the output below,

LOOOOOOOOOOOOP ----> 0 findText: $count replaceText: 0 findText: $filename replaceText: FileName0 findText: $inputFile replaceText: Input0 findText: $outputFile replaceText: Output0 findText: $message replaceText: Message0 findText: $snap replaceText: Snapshot0 LOOOOOOOOOOOOP ----> 1 findText: $count replaceText: 1 findText: $filename replaceText: FileName1 findText: $inputFile replaceText: Input1 findText: $outputFile replaceText: Output1 findText: $message replaceText: Message1 findText: $snap replaceText: Snapshot1 LOOOOOOOOOOOOP ----> 2 findText: $count replaceText: 2 findText: $filename replaceText: FileName2 findText: $inputFile replaceText: Input2 findText: $outputFile replaceText: Output2 findText: $message replaceText: Message2 findText: $snap replaceText: Snapshot2 DONE...

As I am initiating the input file as new in every iteration. So I do find all the $ elements during iteration. It's just that they don't get appended in final file.

Can someone please help here? Thanks a lot.

解决方案

Apparently and surprisingly, Apache POI doesn't have any method to write with append to an existing word document. So above approach doesn't work.

I also tried Apache FileUtils, but it doesn't retain the formating of the word document. I also tried docx4j, but in only works on docx files and its merging utility class is paid.

There is another framework, Aspose Words, which provides much better control and flexibility. It lets you append the content to existing document, with a limitation of 1150 characters. But that's way too much for my requirement to worry for as my writing wasn't more than the set limit.

So I used that to achieve what I wanted to do. It's a success finally.

Thanks for the help @D. Krauchanka

这篇关于Java Apache POI:从.doc文件读取/写入问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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