基于Apache POI创建word报告并导出PDF
报告功能:按业务需要,基于报告模板,实现报告的生成,并导出为word或者pdf。
maven依赖
<!-- apache poi pdf --> <dependency> <groupId>fr.opensagres.xdocreport</groupId> <artifactId>fr.opensagres.poi.xwpf.converter.pdf-gae</artifactId> <version>2.0.3</version> </dependency> <dependency> <groupId>com.itextpdf</groupId> <artifactId>itext-asian</artifactId> <version>5.2.0</version> </dependency>
参考代码:
public static void writeDocxToPdf() throws Exception { //通过word模板生成报告 try (FileInputStream inputStream = new FileInputStream(new File("D:\\work\\test-template.docx"))) { XWPFDocument document = new XWPFDocument(inputStream); //添加标题 XWPFParagraph titleParagraph = document.createParagraph(); DocUtil.setTitleText(titleParagraph, "主机报告"); //段落 XWPFParagraph firstParagraph = document.createParagraph(); XWPFRun run = firstParagraph.createRun(); run.setText("时间范围:" + "2022-05-19 09:40:01 - 2022-07-19 09:40:01"); run.setColor("696969"); run.setFontSize(10); //设置段落背景颜色 CTShd cTShd = run.getCTR().addNewRPr().addNewShd(); cTShd.setVal(STShd.CLEAR); cTShd.setFill("97FFFF"); run.addBreak(); //换行 //段落1:信息表格 XWPFParagraph paragraph = document.createParagraph(); DocUtil.setHeadingText(paragraph, "1.表格信息"); //构建表格 List<String> headers = Arrays.asList("字段1", "字段2", "字段3", "字段4"); XWPFTable table = DocUtil.createTable(document, 8000, headers); List<String> data1 = Arrays.asList("1", "2222222222222222222", "3", "4444444444444444444444"); List<String> data2 = Arrays.asList("5", "6", "7", "8"); List<List<String>> tableData = new ArrayList<>(); tableData.add(data1); tableData.add(data2); DocUtil.setTableData(table, tableData); //段落2:信息表格 XWPFParagraph paragraph2 = document.createParagraph(); DocUtil.setHeadingText(paragraph2, "2.其他信息"); //构建表格 List<String> headers2 = Arrays.asList("字段1", "字段2", "字段3", "字段4", "字段5"); XWPFTable table2 = DocUtil.createTable(document, 9000, headers2); List<String> data3 = Arrays.asList("1", "2222222222222222222", "3", "4444444444444444444444", "55555555555555"); List<String> data4 = Arrays.asList("6", "7", "8", "9", "10"); List<List<String>> tableData2 = new ArrayList<>(); tableData2.add(data3); tableData2.add(data4); DocUtil.setTableData(table2, tableData2); //添加一个段落空行 DocUtil.addBreak(document); addPieChart(document); addScatterChart(document); //会在当前位置创建目录,需要放到大标题后面 // document.createTOC(); String path = "D:\\work\\test-report.docx"; try (FileOutputStream out = new FileOutputStream(path)) { document.write(out); } catch (IOException e) { e.printStackTrace(); } docCovertPdf(document, "test-report.pdf"); document.close(); } }
导出为pdf:
public static void docCovertPdf(XWPFDocument doc, String pdfName) { PdfOptions options = null; //在Linux docker服务器中支持中文 options.fontProvider(new IFontProvider() { @Override public Font getFont(String familyName, String encoding, float size, int style, Color color) { try { BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); Font fontChinese = new Font(bfChinese, size, style, color); if (familyName != null) { fontChinese.setFamily(familyName); } return fontChinese; } catch (Exception e) { return null; } } }); String filePath = "D:\\work\\"; try (OutputStream outPDF = Files.newOutputStream(Paths.get(filePath + pdfName))) { PdfConverter.getInstance().convert(doc, outPDF, options); } catch (Exception e) { e.printStackTrace(); } }
报告效果:
问题:
- 导出pdf支持文字、表格、图片导出,无法支持chart图的导出。
- 导出pdf不能支持“背景颜色”的导出。
基于springboot的代码实现:
https://github.com/luxiaoxun/spring-boot-seed 中 ReportController
开源参考:
https://github.com/opensagres/xdocreport
https://github.com/Sayi/poi-tl
https://github.com/itext/itext7