jasperreports 使用
1.首先引入依赖
<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.13.RELEASE</version></parent><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot</artifactId><version>1.5.13.RELEASE</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>2.1.4</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>com.alibaba.fastjson2</groupId><artifactId>fastjson2</artifactId><version>2.0.58</version></dependency><dependency><groupId>net.sf.jasperreports</groupId><artifactId>jasperreports</artifactId><version>6.19.1</version><exclusions><!-- 排除可能冲突的POI依赖 --><exclusion><groupId>org.apache.poi</groupId><artifactId>poi</artifactId></exclusion><exclusion><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId></exclusion></exclusions><!--使用适合你项目的版本号,这里一定要注意有坑,版本一定要跟你的 JaspersoftStudio报表设计工具匹配,至少要前面两个版本号一直,我的JaspersoftStudio是6.19.1,一开始这里我用的是6.20.6,导致我的报表模版中的交叉表填充数据报异常错误。--></dependency><!-- JasperReports字体库 --><dependency><groupId>net.sf.jasperreports</groupId><artifactId>jasperreports-fonts</artifactId><version>6.20.6</version> <!-- 与JasperReports核心库保持相同的版本号 --></dependency><!-- iText库(如果需要导出为PDF) --><dependency><groupId>com.itextpdf</groupId><artifactId>itextpdf</artifactId><version>5.5.0</version> <!-- 使用适合你项目的版本号 --></dependency><!-- (如果需要导出为Excel) --><dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>3.9</version></dependency></dependencies>
2.使用Jaspersoft Studio
完成模板文件,这里使用的是$F然后属性名 对应java对象定义的属性名,并且要有setget方法,
$F,$P,$V都代表了左边红字显示的变量 F传的是集合List $P一般是map用于相同属性值填充或一个值如总数、总值等。$V是表达式,里面有合并,平均等函数,可以自己定义某个变量作为参数
Filed里面的属性值要设置对应的类型,如String,Integer 都有对应的属性选择
这一步是jrxml文件 可以编译成jasper文件 但我使用java里面的方法 将他编译 (个人觉得比较方便)
3.使用
导出pdf与excel
try {// 设置PDF响应头信息,使浏览器直接显示PDF而不是下载
// response.setContentType("application/pdf");
// response.setHeader("Content-Disposition", "inline; filename=report.pdf");
// response.setCharacterEncoding("UTF-8");// 设置 导出Excel的响应头信息response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");response.setHeader("Content-Disposition", "attachment; filename=report.xlsx");response.setCharacterEncoding("UTF-8");//1.读取模板文件Resource resource = new ClassPathResource("static/moban.jrxml");// 2. 编译 jrxml 到 JasperReport 对象JasperReport jasperReport = JasperCompileManager.compileReport(resource.getInputStream());// 准备数据源(示例数据)List<City> dataList = new ArrayList<>();City data1 = new City();data1.setName("数据1");data1.setValue(1);dataList.add(data1);City data2 = new City();data2.setName("数据2");data2.setValue(2);dataList.add(data2);JRDataSource dataSource = new JRBeanCollectionDataSource(dataList);// 设置数据源(这里使用一个简单的 Map)Map<String, Object> data = new HashMap<>();data.put("name", "Sample Report");data.put("value", "Hello, this is a sample report!");// 4. 填充数据JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, data, dataSource);ServletOutputStream outputStream = response.getOutputStream();//4.导出PDF为文件 导出文件时 不用设置响应头信息
// JasperExportManager.exportReportToPdfStream(jasperPrint,new FileOutputStream("D:\\Temp\\demo.pdf"));
// JasperExportManager.exportReportToPdfStream(jasperPrint,response.getOutputStream());// 导出为excelJRXlsxExporter exporter = new JRXlsxExporter();exporter.setExporterInput(new SimpleExporterInput(jasperPrint));exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(outputStream));exporter.exportReport();response.getOutputStream().flush();response.getOutputStream().close();} catch (JRException e) {e.printStackTrace();}
4.中文问题
因为默认字体无法显示中文,所以要设置