java poi 读取doc,Java Apache POI:从.doc文件读取/写入问题

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,

fQD4R.jpg

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,

DTgQC.jpg

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值