private static void exportPOI(List<SourceCard> cards ) {
//空白文档
XWPFDocument doc =new XWPFDocument();
try {
FileOutputStream fos= new FileOutputStream(new File("D:\\资料卡长编.docx"));
//标题
XWPFParagraph title =doc.createParagraph();
//设置居中
title.setAlignment(ParagraphAlignment.CENTER);
XWPFRun titleRun =title.createRun();
titleRun.setText("资料卡长编");
titleRun.setBold(true);
titleRun.setFontSize(30);
int size =cards.size();
for (int i = 0; i < size; i++) {
XWPFParagraph graph =doc.createParagraph();
XWPFRun run =graph.createRun();
//小标题
run.setText(cards.get(i).getTitle() +"\r");
run.setFontSize(25);
run.setBold(true);
graph =doc.createParagraph();
run =graph.createRun();
//资料正文
run.setText( " 资料正文:\r");
run.setFontSize(20);
run.setBold(true);
//正文
graph =doc.createParagraph();
run =graph.createRun();
//
run.setText(" "+ cards.get(i).getContent()+"\r");
run.setFontSize(16);
//资料来源
graph =doc.createParagraph();
run =graph.createRun();
//
run.setText( " 资料来源:\r");
run.setFontSize(20);
run.setBold(true);
//资料来源
graph =doc.createParagraph();
run =graph.createRun();
//
run.setText(" "+ cards.get(i).getSource()+"\r\\r\\r\\r");
run.setFontSize(16);
}
doc.write(fos);
fos.close();
System.out.println("POI导出doc完毕");
} catch (Exception e) {
e.printStackTrace();
}
}
查看结果