当前位置: 首页 > news >正文

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.中文问题

因为默认字体无法显示中文,所以要设置

http://www.dtcms.com/a/349342.html

相关文章:

  • Vmware centos系统中通过docker部署dify,网络超时和磁盘容量解决方案
  • 解决getLocation获取当前的地理位置,报错:getLocation:fail auth deny及方法封装
  • 容易忽视的TOS无线USB助手配网和接入USB使用: PC和TOS-WLink需要IP畅通,
  • 社群团购平台与定制开发开源AI智能名片S2B2C商城小程序的融合创新研究
  • 解构 Spring Boot “约定大于配置”:从设计哲学到落地实践
  • 在Excel和WPS表格中拼接同行列对称的不连续数据
  • XC95144XL-10TQG144I Xilinx XC9500XL 高性能 CPLD
  • 信贷模型域——清收阶段模型(贷后模型)
  • 关于内存泄漏的一场讨论
  • [Android] 人体细胞模拟器1.5
  • leetcode 238 除自身以外数组的乘积
  • 可信医疗大数据来源、院内数据、病种数据及编程使用方案分析
  • iOS18报错:View was already initialized
  • 生产ES环境如何申请指定索引模式下的数据查看权限账号
  • 【C语言】一些常见概念
  • git开发基础流程
  • 以结构/序列/功能之间的关系重新定义蛋白质语言模型的分类:李明辰博士详解蛋白质语言模型
  • 设计模式4-建造者模式
  • k8s笔记02概述
  • 网络编程--TCP/UDP Socket套接字
  • SciPy科学计算与应用:SciPy插值技术入门-线性与样条插值
  • MySQL 行转列与列转行的实现方式
  • 堆栈面试题之有效的括号
  • 顶升机设计cad+三维图+设计说明书
  • AR智能巡检:重塑消防行业新未来
  • 【Axure高保真原型】嵌套表格_查看附件
  • AR智能巡检:智慧工地的高效安全新引擎
  • zookeeper-znode解析
  • 【P2P】P2P主要技术及RELAY服务实现
  • 前端 Promise 全面深入解析