我正在尝试使用apache POI通过收集arraylist中的一些数据来动态生成word文件,然后将其打印在控制台输出以及word文件中。我能够在控制台和word文件中获得输出,但在每个arraylist元素中,我在末尾添加了一个新的行字符,以便按行方式打印数组元素。在控制台输出中,新的行字符工作,即arraylist元素按行进行,但在生成的word文件中缺少换行符。如何在生成的word文件中保留换行符并删除数组元素末尾的逗号。 注意:arraylist是'result'和'isLinkBroken(new URL(element.getAttribute(“href”)))“是一个返回一些值的函数。相关的代码片段如下:
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();
}
答案 0 :(得分:1)
Word .docx
格式不会将新行(也不会像标签一样的其他空白符号)编码为其原生的ascii表示形式。相反,您需要为那些
如果查看JavaDocs for XWPFRun,您会看到所有空格分隔选项,例如XWPFRun.addTab()和XWPFRun.addCarriageReturn()
您应该阅读good example in the XWPF examples。基本上,要采取文本
This is line one
This is line two
使用XWPF将其编码为.docx
,您应该执行类似
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
答案 1 :(得分:0)
如果您按照Word术语思考,当您点击回车键时,您确实添加了一个新段落。如果你想在行之间中断,你应该为数组中的每个元素添加一个新段落,而不是试图将所有内容保存在一个段落中。
以下是对您的代码的一些修改:
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<String> result = new ArrayList<String>();
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"))));
}
}
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();
for (String elem : result) {
XWPFParagraph para = doc.createParagraph();
XWPFRun pararun=para.createRun();
pararun.setText(elem);
}
doc.write(outStream);
outStream.close();
}
注意:我从字符串中删除了换行符,并在数组列表中添加了Generic。这些不应该改变您的输出(控制台除外)。真正的变化是将创建段落放在循环中。这应该会为文档添加许多段落。
答案 2 :(得分:-1)
而是将列表对象设置为setText方法,您应该遍历列表并使用字符串构建器创建内容。
以下是示例代码:
XWPFRun pararun = para.createRun();
StringBuilder content = new StringBuilder();
for (int j = 0; j < result.size(); j++) {
content.append(result.get(j));
}
pararun.setText(content.toString());
doc.write(outStream);