POI 导出word时不能换行解决

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

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

思路:将需要换行的地方断开,分别生成段,这样就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
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值