1. JasperReports开发流程
使用JasperReports导出pdf报表,开发流程如下:
- 制作报表模板
- 模板编译
- 构造数据
- 填充数据
- 输出文件
2. 安装设计器
TIB_js-studiocomm_6.9.0_windows_x86_64.exe
3. 面板介绍
本工具使用的是eclipse的框架进行开发,基本使用方式与eclipse类同
4. JDBC数据源方式填充数据
//基于jdbc数据源方式填充数据
@Test
public void test2() throws Exception{
Class.forName("com.mysql.jdbc.Driver");
Connection connection =
DriverManager.getConnection("jdbc:mysql://localhost:3306/health",
"root",
"root");
String jrxmlPath = "D:\\itcastProject\\health_parent\\jasperReportsDemo\\src\\main\\resources\\demo1.jrxml";
String jasperPath = "D:\\itcastProject\\health_parent\\jasperReportsDemo\\src\\main\\resources\\demo1.jasper";
//模板编译,编译为后缀为jasper的二进制文件
JasperCompileManager.compileReportToFile(jrxmlPath,jasperPath);
//为模板文件准备数据,用于最终的PDF文件数据填充
Map map = new HashMap();
map.put("company","传智播客");
//填充数据
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperPath,map,connection);
//输出文件
String pdfPath = "D:\\test.pdf";
JasperExportManager.exportReportToPdfFile(jasperPrint,pdfPath);
}
中文修复:
// 1. jrxml 中设置为华文宋体
<font fontName="华文宋体" size="20"/>
// 2. 在resources下添加中文宋体适配文件
5. Javabean方式填充数据
//基于Javabean数据源方式填充数据
@Test
public void test3() throws Exception{
String jrxmlPath = "D:\\itcastProject\\health_parent\\jasperReportsDemo\\src\\main\\resources\\demo2.jrxml";
String jasperPath = "D:\\itcastProject\\health_parent\\jasperReportsDemo\\src\\main\\resources\\demo2.jasper";
//模板编译,编译为后缀为jasper的二进制文件
JasperCompileManager.compileReportToFile(jrxmlPath,jasperPath);
//为模板文件准备数据,用于最终的PDF文件数据填充
Map map = new HashMap();
map.put("company","传智播客");
//Javabean数据源填充,用于填充列表数据
List<Map> list = new ArrayList();
Map map1 = new HashMap();
map1.put("name","入职体检套餐");
map1.put("code","RZTJ");
map1.put("age","18-60");
map1.put("sex","男");
Map map2 = new HashMap();
map2.put("name","阳光爸妈老年健康体检");
map2.put("code","YGBM");
map2.put("age","55-60");
map2.put("sex","女");
list.add(map1);
list.add(map2);
//填充数据
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperPath,map,new JRBeanCollectionDataSource(list));
//输出文件
String pdfPath = "D:\\test.pdf";
JasperExportManager.exportReportToPdfFile(jasperPrint,pdfPath);
}
6. 实际开发过程
6.1 编辑jrxml文件
使用Jaspersoft Studio设计运营数据PDF报表模板文件health_business3.jrxml
6.2 搭建环境
<dependency>
<groupId>net.sf.jasperreports</groupId>
<artifactId>jasperreports</artifactId>
<version>6.8.0</version>
</dependency>
health_backend的template目录中添加模板
将解决中文乱码的文件添加到项目resources文件夹中
6.3 修改页面
<el-button @click="exportPDF">导出PDF</el-button>
exportPDF(){
window.location.href = '/report/exportBusinessReport4PDF.do';
}
6.4 Java代码实现
//导出运营数据到PDF文件并提供客户端下载
@RequestMapping("/exportBusinessReport4PDF")
public Result exportBusinessReport4PDF(HttpServletRequest request, HttpServletResponse response){
try{
Map<String,Object> result = reportService.getBusinessReportData();
//取出返回结果数据,准备将报表数据写入到Excel文件中
List<Map> hotSetmeal = (List<Map>) result.get("hotSetmeal");
String prePath = request.getSession().getServletContext().getRealPath("template") + File.separator;
//动态获取pdf模板文件绝对磁盘路径
String jrxmlPath = prePath + "health_business3.jrxml";
String jasperPath = prePath + "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);
out.flush();
out.close();
return null;
}catch (Exception e){
e.printStackTrace();
return new Result(false, MessageConstant.GET_BUSINESS_REPORT_FAIL);
}
}