我正在尝试使用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"));
}
}
这样做的正确方法是什么?
答案 0 :(得分:-1)
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");