Apache Poi – Java-: How to add text containing blank lines as separate paragraphs to a Word document using Apache POI?

huangapple go评论70阅读模式
英文:

Apache Poi - Java-: How to add text containing blank lines as separate paragraphs to a Word document using Apache POI?

问题

	public static void addingMyParagraphs(XWPFDocument doc, String text) throws InvalidFormatException, IOException {

		XWPFParagraph p = doc.createParagraph();
		XWPFRun run = p.createRun();

		run.setText(text);
		run.setFontFamily("Times new Roman");
	}

	public void CreatingDocument() throws IOException, InvalidFormatException {
		String theText = myText.getText();
		addingMyParagraphs(doc, theText);

		FileOutputStream output = new FileOutputStream("MyDocument.docx");
		doc.write(output);
		output.close();
	}
英文:

I am unable to add text containing blank lines as separate paragraphs to a word document.

If I try to add the following text that contains 3 different paragraphs.

  1. Some text here.
  2. Another text here.
  3. Another one here.

what I get is 1. Some text here. 2. Another text here. 3. Another one here. as if they were the same paragraph.

Is it possible to add a text containing blank lines as separate paragraphs to a Word document using Apache POI?

	public static void addingMyParagraphs(XWPFDocument doc, String text) throws InvalidFormatException, IOException {

		XWPFParagraph p = doc.createParagraph();
		XWPFRun run = p.createRun();

		run.setText(text);
		run.setFontFamily("Times new Roman");
	}

--In the method below MyText variable is a textArea variable that's part of a javaFx application.

	public void CreatingDocument() throws IOException, InvalidFormatException {
		String theText = myText.getText();
		addingMyParagraphs(doc, theText);

		FileOutputStream output = new FileOutputStream("MyDocument.docx");
		doc.write(output);
		output.close();
	}
}

答案1

得分: 1

你需要将你的文本分割成“段落”,然后将每个段落分别添加到你的 WORD 文档中。这与 JavaFX 无关。

以下是一个示例,使用 文本块 来模拟输入到 [JavaFX] TextArea 中的文本。代码之后是解释。

import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;

public class PoiWord0 {

    public static void main(String[] args) {
        String text = """
                1. 这里是一些文本。

                2. 这里是另一些文本。
                
                3. 这里是另一个。
                """;
        String[] paras = text.split("(?m)^[ \\t]*\\r?\\n");
        try (XWPFDocument doc = new XWPFDocument();
             FileOutputStream output = new FileOutputStream("MyDocument.docx")) {
            for (String para : paras) {
                XWPFParagraph p = doc.createParagraph();
                XWPFRun run = p.createRun();
                run.setText(para.stripTrailing());
            }
            doc.write(output);
        }
        catch (IOException xIo) {
            xIo.printStackTrace();
        }
    }
}

我假设段落分隔符是一个空行,所以我在空行上分割文本。这仍然在数组的每个元素中保留了尾随的换行字符。我使用 stripTrailing() 来去除那个换行符。

现在我有了一个段落的数组,所以我只需为每个数组元素在 [WORD] 文档中添加一个新段落。

请注意,上述代码是使用 JDK 15 编写的。

文本分割的正则表达式来自于名为 Remove empty line from a multi-line string with Java 的 StackOverflow 问题。

try-with-resources 是从 Java 7 开始添加的。

stripTrailing() 是在 JDK 11 中添加的。

英文:

You need to split your text into "paragraphs" and add each paragraph separately to your WORD document. This has nothing to do with JavaFX.

Here is an example that uses text blocks to simulate the text that is entered into the [JavaFX] TextArea. Explanations after the code.

import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;

public class PoiWord0 {

    public static void main(String[] args) {
        String text = """
                1. Some text here.

                2. Another text here.
                
                3. Another one here.
                """;
        String[] paras = text.split("(?m)^[ \\t]*\\r?\\n");
        try (XWPFDocument doc = new XWPFDocument();
             FileOutputStream output = new FileOutputStream("MyDocument.docx")) {
            for (String para : paras) {
                XWPFParagraph p = doc.createParagraph();
                XWPFRun run = p.createRun();
                run.setText(para.stripTrailing());
            }
            doc.write(output);
        }
        catch (IOException xIo) {
            xIo.printStackTrace();
        }
    }
}

I assume that a paragraph delimiter is a blank line, so I split the text on the blank lines. This still leaves the trailing newline character in each element of the array. I use stripTrailing() to remove that newline.

Now I have an array of paragraphs, so I simply add a new paragraph to the [WORD] document for each array element.

Note that the above code was written using JDK 15.

The regex for splitting the text came from the SO question entitled Remove empty line from a multi-line string with Java

try-with-resources was added in Java 7.

stripTrailing() was added in JDK 11

huangapple
  • 本文由 发表于 2020年9月27日 10:48:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/64084310.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定