gpt4 book ai didi

java - 如何在java中的.doc文件中使用POI hwpfdocument写入大量文本

转载 作者:行者123 更新时间:2023-11-29 09:21:29 27 4
gpt4 key购买 nike

我是 POI API 的新手,因此,每当我使用 HWPFDocument 类在 HWPF 文档创建的 .doc 文件中写下文本时,生成的 doc 文件只有一个字母而不是整个文本,我写了。

因此,请给出我如何向其中添加更多文本的答案。

这是代码:

/**
* Create a POIFSFileSystem from an InputStream.
* Test.doc is an empty doc with no contents
*/
POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream("test.doc"));

/**
* Horrible word Document Format
*/
HWPFDocument hwpfDocument = new HWPFDocument(fs);
/**
* range is used for getting the range of the document except header and footer
*/

Range range = hwpfDocument.getRange();
range.numParagraphs();
/**
* creating Paragraph
*/
/**
* Inserts a paragraph into the end of this range.
* ParagraphProperties -> for creating Paragraph, index of the paragraph
*/
/*Paragraph paragraph1 = range.insertBefore(new ParagraphProperties(), 0);

paragraph1.setJustification((byte) 0); // 0-left, 1-center, 2-right, 3-left and right*/
CharacterRun characterRun1 = range.insertBefore("Document");
/**
* setting the font size
*/
characterRun1.setFontSize(2 * 12); // takes as an argument half points
/**
* making Text Bold Italic
*/
characterRun1.setBold(true);
characterRun1.setItalic(true);
characterRun1.setColor(111);
characterRun1.setData(true);
//characterRun1.replaceWith("Document");

hwpfDocument.write(new FileOutputStream("hpwf-create-doc.doc", true));

最佳答案

如果可以,从使用 HWPFDocument 切换到 XWPFDocument(生成 .docx 而不是 .doc)。 .docx 格式在底层更明智(它基于 XML 而不是二进制),因此 POI 通常用它做得更好。

否则,请尝试使用更简单的模板文档。 word 文件格式对各种位都非常挑剔,您可能遇到过这样的情况,即 POI 不知道需要更新文件中的某些内容,然后 word 默默反对。

关于java - 如何在java中的.doc文件中使用POI hwpfdocument写入大量文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6149298/

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