[英]How to set change font colors by POI
I use below code to update MS Word by using POI in my selenium scripts.我使用以下代码在我的 selenium 脚本中使用 POI 来更新 MS Word。
public class WordAutomation {
public static String projectpath = System.getProperty("user.dir");
public static FileOutputStream out;
public static String ScreenshotPath = projectpath+"\\Screenshots\\";
public static XWPFDocument docx;
public static XWPFRun run;
public static String wordFile;
public static void main(String[] args) throws IOException {
docx = new XWPFDocument();
run = docx.createParagraph().createRun();
wordFile = ScreenshotPath+"ScreenshotFile.docx";
out = new FileOutputStream(wordFile);
run.setText("Test Started");
run.addBreak();
run.setText("Its a Pass scenario");
run.addBreak();
run.setColor("ff0000");//Change the font color to red
run.setText("Its a Fail scenario");
run.addBreak();
run.setColor("000000");//back to black again
run.setText("Its a Pass scenario");
docx.write(out);
out.flush();
out.close();
docx.close();
}
}
I need output like below我需要如下所示的 output
but i am getting output like below但我得到如下所示的 output
Please help me to solve my issue.请帮我解决我的问题。 Actually i want to save the document to implement the changes and continue further.
实际上我想保存文档以实施更改并继续。
It looks like a new run has to be created for each change in style;看起来每次样式更改都必须创建一个新的运行; see link below.
请参阅下面的链接。 This works:
这有效:
docx = new XWPFDocument();
wordFile = screenshotPath+"ScreenshotFile.docx";
out = new FileOutputStream(wordFile);
p = docx.createParagraph();
run = p.createRun();
run.setText("Test Started");
run.addBreak();
run = p.createRun();
run.setText("Its a Pass scenario");
run.addBreak();
run = p.createRun();
run.setColor("FF0000");
run.setText("Its a Fail scenario");
run.addBreak();
run = p.createRun();
run.setText("Its a Pass scenario");
run.addBreak();
docx.write(out);
out.flush();
out.close();
docx.close();
See also: https://stackoverflow.com/a/41681461/127971另请参阅: https://stackoverflow.com/a/41681461/127971
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.