使用Apache POI 3.8(HWPF)格式化文本

时间:2012-07-06 11:02:31

标签: apache-poi hwpf

我正在尝试使用Apache POI 3.8在文档中插入以下文本:

  

[粗体] [标准],

但输出文档有:

  

[粗体] [正常]

代码:

import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.*;
import java.io.*;

public class Main {
    public static void main(String[] args) throws IOException {
        final HWPFDocument doc = new HWPFDocument(new FileInputStream("empty.dot"));

        final Range range = doc.getRange();
        final CharacterRun cr1 = range.insertAfter("[Bold]");
        cr1.setBold(true);

        final CharacterRun cr2 = cr1.insertAfter("[Normal]");
        cr2.setBold(false);

        doc.write(new FileOutputStream("output.doc"));
    }
}

这样做的正确方法是什么?

1 个答案:

答案 0 :(得分:-1)

我是这样做的。使用POI 3.11

paragraph = doc.createParagraph();
paragraph.setStyle(DOG_HEAD_STYLE);
XWPFRun tmpRun = paragraph.createRun();
tmpRun.setText("non bold text ");

tmpRun = paragraph.createRun();
tmpRun.setBold(true);
tmpRun.setText("bold text");
tmpRun = paragraph.createRun();
tmpRun.setBold(false);
tmpRun.setText(" non bold text again");