<dependency>
<groupId>net.sf.jasperreports</groupId>
<artifactId>jasperreports</artifactId>
<version>6.8.0</version>
</dependency>
//导出运营数据到pdf并提供客户端下载
@RequestMapping("/exportBusinessReport4PDF")
public Result exportBusinessReport4PDF(HttpServletRequest request,
HttpServletResponse response) {
try {
Map<String, Object> result = reportService.getBusinessReportData();
//取出返回结果数据,准备将报表数据写入到PDF文件中
List<Map> hotSetmeal = (List<Map>) result.get("hotSetmeal");
//动态获取模板文件绝对磁盘路径
String jrxmlPath =
request.getSession().getServletContext().getRealPath("template") +
File.separator + "health_business3.jrxml";
String jasperPath =
request.getSession().getServletContext().getRealPath("template") +
File.separator + "health_business3.jasper";
//编译模板
JasperCompileManager.compileReportToFile(jrxmlPath, jasperPath);
//填充数据---使用JavaBean数据源方式填充
JasperPrint jasperPrint =
JasperFillManager.fillReport(jasperPath,result,
new
JRBeanCollectionDataSource(hotSetmeal));
ServletOutputStream out = response.getOutputStream();
response.setContentType("application/pdf");
response.setHeader("content-Disposition",
"attachment;filename=report.pdf");
//输出文件
JasperExportManager.exportReportToPdfStream(jasperPrint,out);
return null;
} catch (Exception e) {
e.printStackTrace();
return new Result(false, MessageConstant.GET_BUSINESS_REPORT_FAIL);
}
}
原文:https://www.cnblogs.com/shangyunlin/p/12489241.html