java poi word换行符,在使用Apache POI的Word文件生成中保留换行符

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 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();

}

解决方案

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

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

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");

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值