使用apache POI在word文件生成中保留换行符 [英] retain newline in word file generation using apache POI
问题描述
我正在尝试使用 apache POI 通过在 arraylist 中收集一些数据然后将其打印在控制台输出以及 word 文件中来动态生成 word 文件.我能够在控制台和 word 文件中获得输出,但在每个 arraylist 元素中,我在末尾添加了一个新行字符,以便逐行打印数组元素.在控制台输出中,换行符起作用,即 arraylist 元素逐行出现,但在生成的 word 文件中缺少换行符.如何在生成的 word 文件中保留换行符并删除数组元素末尾的逗号.注意:arraylist 是结果",isLinkBroken(new URL(element.getAttribute("href")))"是一个返回一些值的函数.相关代码片段如下:
I am trying to use apache POI to dynamically generate a word file by collecting some data in an arraylist and then printing it in the console output as well as the word file. I am able to get the output in console as well as the word file, but inside each arraylist element I have added a new line character at the end so that the array elements are printed linewise. In the console output the new line character works i.e. the arraylist elements come linewise but in the generated word file the line break is missing.How can I retain the line breaks in the generated word file and remove the comma at the end of the array elements. NOTE: the arraylist is 'result' and "isLinkBroken(new URL(element.getAttribute("href")))" is a function that returns some value.The concerned code snippet is given below :
protected void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException {
String url= request.getParameter("url");
System.setProperty("webdriver.chrome.driver", "H:\\suraj\\sftwr\\chromedriver_win32\\chromedriver.exe");
ChromeDriver ff = new ChromeDriver();
ff.get("http://"+url);
ArrayList result = new ArrayList();
List<WebElement> allImages = findAllLinks(ff);
int i=0;
System.out.println("Total number of elements found " + allImages.size());
for( WebElement element : allImages){
try {
if(!isLinkBroken(new URL(element.getAttribute("href"))).equals("OK")) {
i++;
System.out.println("inside"+i);
System.out.println("URL: " + element.getAttribute("href")+ " returned " + isLinkBroken(new URL(element.getAttribute("href"))));
result.add(i+" URL: " + element.getAttribute("href")+ " returned " + isLinkBroken(new URL(element.getAttribute("href")))+"\n");
}
}
catch(Exception exp) {
System.out.println("outside");
System.out.println("At " + element.getAttribute("innerHTML") + " Exception occured -> " + exp.getMessage());
}
}
System.out.println("OUTPUT");
System.out.println(result.toString());
FileOutputStream outStream=new FileOutputStream("H:\\suraj\\InactiveURL\\test.docx");
XWPFDocument doc=new XWPFDocument();
XWPFParagraph para = doc.createParagraph();
para.setAlignment(ParagraphAlignment.LEFT);
XWPFRun pararun=para.createRun();
pararun.setText(result.toString());
doc.write(outStream);
outStream.close();
}
推荐答案
Word .docx
格式不会将换行符(或其他空格符,如制表符)编码为它们的原生 ascii 表示.相反,您需要为那些使用额外的 XML 标记
The Word .docx
format doesn't encode Newlines (nor other whitespace breaks like tabs) as their native ascii representations. Instead, you need to use additional XML tags for those
如果您查看 XWPFRun 的 JavaDocs,你会看到所有的空格中断选项,比如 XWPFRun.addTab() 和 XWPFRun.addCarriageReturn()
If you look at the JavaDocs for XWPFRun, you'll see all the whitespace break options, such as XWPFRun.addTab() and XWPFRun.addCarriageReturn()
有一个 您应该通读的 XWPF 示例中的好示例.基本上,取文本
There's a good example in the XWPF examples which you should read through. Basically though, to take the text
This is line one
This is line two
并使用 XWPF 将其编码为 .docx
,您应该执行类似的操作
And encode that into .docx
using XWPF, you should do something like
XWPFParagraph p1 = doc.createParagraph();
XWPFRun r1 = p1.createRun();
r1.setText("This is line one");
r1.addCarriageReturn();
r1.setText("This is line two");
如果您从一个文本块开始,您应该将其拆分为换行符.接下来,使用单独的 run.setText
调用添加每个分割线,并在每个分割线之间执行 run.addCarriageReturn
If you're starting from a block of text, you should split that on newlines. Next, add each split line with a separate run.setText
call, and do a run.addCarriageReturn
between each
这篇关于使用apache POI在word文件生成中保留换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!