栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringBoot(十六)_springboot整合JasperReport6.6.0

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringBoot(十六)_springboot整合JasperReport6.6.0

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

1.下载 安装 Jaspersoft Studio

下载地址:https://community.jaspersoft.com/community-download

我下载的就是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;    
    @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:https://github.com/runzhenghengbin/SpringBoot

原文出处:https://www.cnblogs.com/zhenghengbin/p/10268495.html 

作者:Kevin_zheng 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/239313.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号