报告生成:
以下代码驻留在 servlet 中,并生成要下载的“letter.docx”word 文档和 C 语言中的“pika.pdf”文件:
我可以看到我在 pika 中定义的背景图像,但在“字母”中看不到。
InputStream is = request.getServletContext().getResourceAsStream("/resources/reports/" +name);
JasperReport jr = JasperCompileManager.compileReport(is);
JasperPrint jp = JasperFillManager.fillReport(jr, params, ds);
JRExporter exp = new JRDocxExporter();
exp.setParameter(JRExporterParameter.JASPER_PRINT, jp);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
exp.setParameter(JRExporterParameter.OUTPUT_STREAM, bos);
exp.exportReport();
JasperExportManager.exportReportToPdfFile(jp, "C:\\pika.pdf");
byte[] bytes = bos.toByteArray();
response.reset();
response.setContentType("application/octet-stream");
response.setHeader("Content-disposition", "attachment; filename=\"letter.docx\"");
response.getOutputStream().write(bytes);
response.getOutputStream().flush();
response.getOutputStream().close();