在项目中输出运营数据PDF报表
本小节我们将在项目中实现运营数据的PDF报表导出功能。
设计PDF模板文件#
使用Jaspersoft Studio设计运营数据PDF报表模板文件health_business3.jrxml,设计后的效果如下:
在资源中已经提供好了此文件,直接使用即可。
搭建环境#
第一步:在health_common工程的pom.xml中导入JasperReports的maven坐标
<dependency> <groupId>net.sf.jasperreports</groupId> <artifactId>jasperreports</artifactId> <version>6.8.0</version> </dependency>
第二步:将资源中提供的模板文件health_business3.jrxml复制到health_backend工程的template目录下
第三步:将解决中问题的相关资源文件复制到项目中
修改页面#
修改health_backend工程的report_business.html页面,添加导出PDF的按钮并绑定事件
Java代码实现#
在health_backend工程的ReportController中提供exportBusinessReport4PDF方法
//导出运营数据到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); } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)
2019-07-15 迭代器、生成器、send方法、send与return的异同点、生成器表达式、常用的内置方法