博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SpringBoot(十六)_springboot整合JasperReport6.6.0
阅读量:7070 次
发布时间:2019-06-28

本文共 3799 字,大约阅读时间需要 12 分钟。

现在项目上要求实现套打,结果公司里有个人建议用JaperReport进行实现,就进入这个东西的坑中。好歹经过挣扎现在已经脱离此坑中。现在我也是仅能实现读取数据库数据转成pdf进行展示,包括中文的展示。于是记录下整个过程。

1.下载 安装 Jaspersoft Studio

下载地址:

我下载的就是6.6.0这个版本,Jasper Report 分为专业版(收费)和社区版(免费),这里下载的社区版本。

2.设计模板

对这个模板设计我也不是很熟悉,这里我就不展开说明了。大家自行设计吧

2.1 导入并设置字体

这里需要注意一点就是,这个设计出的不显示中文,需要导入字体。

点击window->Preferences->jaspersoft Studio->font->add

设置完成后,点击Finish.

2.2 设计模板选择设置的myfont字体

查看jrxml文件后,设置后会多出font标签

                                

2.3 导出myfont字体jar包

点击window->Preferences->jaspersoft Studio->font

选择myfont 点击export 导出。这里我保存为kevin.jar

2.4 将kevin.jar 安装到本地maven库

mvn install:install-file -Dfile=kevin.jar -DgroupId=com.kevin -DartifactId=myfont -Dversion=1.0.0 -Dpackaging=jar

至此,前期的准备工作都已经完成。

3.构建springboot项目

这里我使用的版本是 2.1.1.RELEASE

3.1 pom文件

org.springframework.boot
spring-boot-starter-jdbc
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-freemarker
mysql
mysql-connector-java
runtime
org.springframework.boot
spring-boot-starter-test
test
net.sf.jasperreports
jasperreports
6.6.0
com.kevin
myfont
1.0.0

注意 ,我这里引用的是我设置的com.kevin.myfont版本,个人根据自己步骤2.4设置的情况进行修改

3.2 application.yml

# Server settingsserver:  port: 8080# SPRING PROFILESspring:  http:    encoding.charset: UTF-8    encoding.enable: true    encoding.force: true  datasource:    url: jdbc:mysql://127.0.0.1:3306/kevin?characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8    username: root    password: 123456    driver-class-name: com.mysql.jdbc.Driver

3.3 ReportController 代码

@RestControllerpublic class ReportController {    @Resource    private DataSource dataSource;    /**     * 转换为pdf展示     *     * @param reportName     * @param parameters     * @param response     * @throws SQLException     * @throws ClassNotFoundException     * @throws JRException     * @throws IOException     */    @GetMapping("/{reportName}")    public void getReportByParam(            @PathVariable("reportName") final String reportName,            @RequestParam(required = false) Map
parameters, HttpServletResponse response) throws SQLException, ClassNotFoundException, JRException, IOException { parameters = parameters == null ? new HashMap<>() : parameters; //获取文件流 ClassPathResource resource = new ClassPathResource("jaspers" + File.separator + reportName + ".jasper"); InputStream jasperStream = resource.getInputStream(); JasperReport jasperReport = (JasperReport) JRLoader.loadObject(jasperStream); JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, dataSource.getConnection()); // JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, null, new JREmptyDataSource()); response.setContentType("application/pdf"); response.setHeader("Content-Disposition", "inline;"); final OutputStream outputStream = response.getOutputStream(); JasperExportManager.exportReportToPdfStream(jasperPrint, outputStream); }}

3.4 完整目录结构

4.运行效果

启动项目,运行http://localhost:8080/demo

完整代码

github:

玩的开心!

转载地址:http://cyell.baihongyu.com/

你可能感兴趣的文章
NumPy 高级索引和数组概念
查看>>
数据库智能管理助手-CloudDBA
查看>>
双星闪耀,开创先河!蚂蚁金服安全实验室首次同时亮相BlackHat Asia 以及CanSecWest国际安全舞台...
查看>>
Windows7 中配置IIS7的方法(HTTP 错误 404.3 - Not Found)
查看>>
如何高效的将excel导入sqlserver?(转)
查看>>
ASP.NET MVC实践系列8-对查询后分页处理的解决方案
查看>>
让python bottle框架支持jquery ajax的RESTful风格的PUT和DELETE等请求
查看>>
Volley
查看>>
马斯克的另一番“威胁论”:人类将成为人工智能的“宠物”
查看>>
Hadoop多用户资源管理–Fair Scheduler介绍与配置(Yarn)
查看>>
批量创建用户及密码
查看>>
redis cluster (1) 原理与基本操作
查看>>
数据库的三大范式以及五大约束
查看>>
Nginx学习笔记(二)——搭建Web服务器
查看>>
BGP之间建立邻居
查看>>
在DOS下配计算机的ip
查看>>
网摘--2014年5月12日
查看>>
python作业
查看>>
浅析如何在Linux系统中如何安装软件
查看>>
windows常用命令(快捷命令篇)
查看>>