POI 导出word时不能换行解决

来源:互联网 发布:曼德拉效应 知乎 编辑:程序博客网 时间:2024/05/17 19:18

今天遇到这个问题,郁闷了很久,还好终于解决了,在此给大家分享下经验!

其它的就不说了,上代码:

思路:将需要换行的地方断开,分别生成段,这样就ok了


public void buildWord(String title, String content, String exportPath) {
FileOutputStream out = null;
try {
XWPFDocument doc = new XWPFDocument();
XWPFParagraph head = doc.createParagraph();
head.setAlignment(ParagraphAlignment.LEFT);
head.setVerticalAlignment(TextAlignment.TOP);



XWPFRun r1 = head.createRun();

r1.setBold(true);
r1.setText(title);
r1.setBold(true);
r1.setFontFamily("Verdana");
r1.setFontSize(12);

XWPFParagraph body = null;
String[] subContent = content.split("\r");
int counter = 0;
for(String str : subContent)
{
counter++;
body = doc.createParagraph();
body.setAlignment(ParagraphAlignment.LEFT);
body.setSpacingAfter(0);
body.setSpacingBefore(0);
XWPFRun r4 = body.createRun();
r4.setText(str);
r4.setFontSize(10);
r4.setFontFamily("Verdana");
}
System.out.println("total phase:"+counter);



String tmpName = SDF.format(new Date());
out = new FileOutputStream(exportPath+File.separator+tmpName+".docx");
doc.write(out);

} catch (Exception e) {
e.printStackTrace();
}finally
{
try {
if(out != null)
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}


System.out.println("done!");
}

0 0
原创粉丝点击