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

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.xdnf.cn/news/18664.html

相关文章:

  • 解锁处暑健康生活
  • 企业级监控可视化系统 Prometheus + Grafana
  • LoRA(低秩适应,Low-Rank Adaptation)的 alpha 参数解析(54)
  • 雷卯针对香橙派Orange 4G-IOT开发板防雷防静电方案
  • kafka 原理详解
  • 【OpenAI】ChatGPT-4o-latest 真正的多模态、长文本模型的详细介绍+API的使用教程!
  • 深入理解 Python Scapy 库:网络安全与协议分析的瑞士军刀
  • ES6/ES2015 - ES16/ES2025
  • 在压力测试中如何确定合适的并发用户数?
  • 挖币与区块链技术有怎样的联系?
  • 基于 Prometheus+Alertmanager+Grafana 打造监控报警后台(一)-Prometheus介绍及安装
  • DMP-Net:面向脑组织术中成像的深度语义先验压缩光谱重建方法|文献速递-深度学习人工智能医疗图像
  • PyTorch实战(1)——深度学习概述
  • 阿里:基于设计逻辑的LLM数据合成
  • crc16是什么算法
  • C++ 指针与引用面试深度解析
  • STM32项目分享:基于STM32的智能洗衣机
  • 开源大模型天花板?DeepSeek-V3 6710亿参数MoE架构深度拆解
  • 微软恶意软件删除工具:官方免费的系统安全防护利器
  • 网络编程1-基本概念、函数接口
  • 2.1.5 数学与其他
  • VUE 的弹出框实现图片预览和视频预览
  • C++数据结构之二叉搜索树
  • AEB 强制来临,东软睿驰Next-Cube-Lite有望成为汽车安全普惠“破局器”
  • macbook国内源安装rust
  • 【AGI使用教程】GPT-OSS 本地部署(2)
  • 【AMBA总线互联IP】
  • 自然语言处理——07 BERT、ELMO、GTP系列模型
  • python文件import找不到其它目录的库解决方案
  • Python爬虫第四课:selenium自动化