java - 如何使用 apache poi 在 docx 中写入混合文本(普通和下标)?

标签 java ms-word apache-poi

我会尽力解释我需要什么。 我有一个包含正常大小和下标文本(例如 oxygene O2)的文本,我想使用 apache poi 3.9 库将其写入 docx 文件。我得到所有文本正常大小或下标。有没有办法做到这一点? 这是我的代码:

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

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

public class ApachePOI {    
    public static void main(String[] args) {
    XWPFDocument document = new XWPFDocument();
    XWPFParagraph paragraph = document.createParagraph();
    XWPFRun run = paragraph.createRun();

    run.setText("Oxygene - O");
    run.setSubscript(VerticalAlign.SUBSCRIPT);
    run.setText("2");

    try {
        FileOutputStream fos = new FileOutputStream("test.docx");
        document.write(fos);
        fos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

最佳答案

是的,试试这段代码-

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

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

public class ApachePOI {    
    public static void main(String[] args) {

    XWPFDocument document = new XWPFDocument(); 
    XWPFParagraph paragraphOne = document.createParagraph();

    paragraphOne.setAlignment(ParagraphAlignment.CENTER);

    XWPFRun paragraphOneRunOne = paragraphOne.createRun();
    paragraphOneRunOne.setFontSize(25);
    paragraphOneRunOne.setBold(true);
    paragraphOneRunOne.setText("Oxygene - O");

    XWPFRun paragraphOneRunTwo = paragraphOne.createRun();
    paragraphOneRunTwo.setFontSize(17);
    paragraphOneRunTwo.setBold(true);
    paragraphOneRunTwo.setSubscript(VerticalAlign.SUBSCRIPT);
    paragraphOneRunTwo.setText("2");

    try {
        FileOutputStream fos = new FileOutputStream("C://test.docx");
        document.write(fos);
        fos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

enter image description here

关于java - 如何使用 apache poi 在 docx 中写入混合文本(普通和下标)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21616016/

相关文章:

java - Java LinkedList 能否在多线程中安全读取?

excel - 从 Word 调用 Excel VBA

ms-word - VSTO字2007 : How can a control be programmatically moved above/below the range of an existing control?

ms-word - Microsoft Office 如何知道文档是否是从 Internet 下载的?

java - 无法从文本单元格获取数值

java - 发布类加载器时,什么时候对单例调用 finalize?

java - 如何在导致应用程序挂起的 HtmlUnit 2.9 中禁用反向 ajax 轮询?

java - Jenkins 无法为我的 Selenium 项目编译 Maven

java - 单击屏幕时更改 TextView 值

java - 我想读取 Excel 并写入文本,以便我的第一个单元格将是键,其余单元格是值