我最近一直在使用 Apache POI,但我不知道如何设置这样的字符串:“Hello World”。这就是我一直在尝试的
XWPFDocument document = new XWPFDocument();
String path = System.getProperty("user.home")+ "/Desktop/"+ array.get(0); //"array" is an ArrayList<String>
path = path.replace("\\","/");
XWPFParagraph paragraph = document.createParagraph();
XWPFRun run = paragraph.createRun();
String str1 = "Price: ";
String str2 = array.get(1); // This needs to be Bold
run.setFontSize(9);
run.setFontFamily("Arial");
run.setText(str1);
run.setBold(true);
run.setText(str2);
paragraph.setSpacingBetween(1);
paragraph.setAlignment(ParagraphAlignment.RIGHT);
try {
FileOutputStream output = new FileOutputStream(ruta);
document.write(output);
output.close();
document.close();
}catch(Exception e) {
e.printStackTrace();
}
我知道“run.setBold(true)”应该将其应用于整个段落,但它是我在Word文档中找到的唯一东西,所以我需要一些帮助来解决这个问题。感谢您的帮助。
一般来说,一段是一段,一段是一段;它们是不同的东西。您可以将其设置为单个单词、相邻单词或整个段落。唯一重要的是 a) 如果你想“粗体”某些东西,那么 b) 你需要“粗体”相应的运行。
令人困惑的是,在 POI 中,您用“段落”创建“运行”:(
...但是...
您可以在SAME段落中进行多次运行 - 具有不同的属性。
例如:
XWPFParagraph p = doc.createParagraph();
XWPFRun r1 = p.createRun();
r1.setText("Some Text");
r1.setBold(true);
r2 = p.createRun();
r2.setText("Goodbye");
我还没有尝试过这段代码,但我相信“Some Text”会是粗体,而“Goodbye”不会。您还可以尝试不同的语法,看看哪种最适合您。