在使用Apache POI的Word文件生成中保留换行符 [英] retain newline in word file generation using apache POI

查看:1194
本文介绍了在使用Apache POI的Word文件生成中保留换行符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用apache POI通过在arraylist中收集一些数据,然后将其打印在控制台输出以及word文件中来动态生成word文件.我可以在控制台以及word文件中获得输出,但是在每个arraylist元素内部,我都在末尾添加了一个新的换行符,以便按行打印阵列元素.在控制台输出中,新的换行符起作用,即arraylist元素按行排列,但是在生成的Word文件中缺少换行符.如何将换行符保留在生成的Word文件中,并删除数组元素末尾的逗号. 注意:arraylist为'result',而"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 -&gt; " + 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 ,您将看到所有空格符选项,例如

If you look at the JavaDocs for XWPFRun, you'll see all the whitespace break options, such as XWPFRun.addTab() and XWPFRun.addCarriageReturn()

有一个

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆