zoukankan      html  css  js  c++  java
  • iReport(模版) 与Jasper(数据填充)生成pdf文档

      报表模板生成软件:iReport 、 润乾、水晶。 

    一、Jaspersoft iReport Desiginer 5.60 的使用

      1、软件jar包的下载地址与配置

      百度云盘下载链接: https://pan.baidu.com/s/1Ln9ewKMhYuau1bG9EgdUNQ  密码:cspl

      此软件最高仅支持1.7版本JDK,如果是1.8版本JDK,需要安装1.7以下的JDK,然后修改配置文件

      配置文件位置 : 安装目录iReport-5.6.0etcireport.conf,增加以下配置

    jdkhome="C:Program FilesJavajdk1.7.0_80"

      2、创建模版

    如果需要删除不需要的区域,右键选中该区域,然后选择Delete Band

    以下三个区域为必须

    如需添加文本控件,选择下图中的Static Text

    如果控件中有中文,需要添加支持中文的iTextAsainJAR,按照下图所示进行配置

     

    指定字体和属性设置,所有使用中文的控件,都必须按照下图所示进行配置.

    创建数据源,如果连接Oracle数据库,需要手动指定数据库驱动,配置方式请参考上面配置iTextAsainJAR包的步骤.

    指定SQL语句

    Fields中拖拽需要的数据到模版中Detail区域

    此时可以用鼠标拖动控件到合适的区域,如果需要修改对应的表头,可以直接在Page Header区域进行修改,如下图所示

    如果需要添加动态参数,右键单击 Parameter进行添加

    修改Parameterkey

    修改之后,拖动到合适的区域

    至此,版创建成功

    如需预览,点击预览按钮即可

    二、 使用Jasper生成PDF

    1、导入依赖

     1 ​​<!-- itext -->
     2 ​​<dependency>
     3   ​​​<groupId>com.lowagie</groupId>
     4 ​​​  <artifactId>itext</artifactId>
     5   ​​​<version>2.1.7</version>
     6 ​​</dependency>
     7 ​​<dependency>
     8 ​​​  <groupId>com.itextpdf</groupId>
     9   ​​​<artifactId>itext-asian</artifactId>
    10   ​​​<version>5.2.0</version>
    11 ​​</dependency>
    12  
    13 ​​<!-- groovy -->
    14 ​​<dependency>
    15   ​​​<groupId>org.codehaus.groovy</groupId>
    16   ​​​<artifactId>groovy-all</artifactId>
    17 ​​​  <version>2.2.0</version>
    18 ​​</dependency>
    19  
    20 ​​<!-- jasperreport -->
    21 ​​<dependency>
    22 ​​​  <groupId>net.sf.jasperreports</groupId>
    23 ​​​  <artifactId>jasperreports</artifactId>
    24 ​​​  <version>5.2.0</version>
    25   ​​​<exclusions>
    26 ​​​​    <exclusion>
    27       ​​​​​<groupId>com.lowagie</groupId>
    28 ​​​​​      <artifactId>itext</artifactId>
    29 ​​​​    </exclusion>
    30 ​​​  </exclusions>
    31 ​​</dependency>

    拷贝iReport设计好的模版到工程中

    2、前台代码

    增加按钮
    {
        ​​id : 'button-export',
    ​​    text : '导出PDF',
    ​​    iconCls : 'icon-undo',
    ​​    handler : doExportPDF
    ​}
    绑定按钮点击事件
    ​function doExportPDF() {
        ​​window.location.href = "../../areaAction_exportPDF.action"
    ​}

    3、java代码

    实现Action
     1 @Autowired
     2    private DataSource dataSource;
     3  
     4    @Action("areaAction_exportPDF")
     5    public String exportPDF() throws Exception {
     6  
     7        // 读取 jrxml 文件
     8        String jrxml = ServletActionContext.getServletContext()
     9                .getRealPath("/jasper/area.jrxml");
    10        // 准备需要数据
    11        Map<String, Object> parameters = new HashMap<String, Object>();
    12        parameters.put("company", "黑马程序员");
    13        // 准备需要数据
    14        JasperReport report = JasperCompileManager.compileReport(jrxml);
    15        JasperPrint jasperPrint = JasperFillManager.fillReport(report,
    16                parameters, dataSource.getConnection());
    17  
    18        HttpServletResponse response = ServletActionContext.getResponse();
    19        OutputStream ouputStream = response.getOutputStream();
    20        // 设置相应参数,以附件形式保存PDF
    21        response.setContentType("application/pdf");
    22        response.setCharacterEncoding("UTF-8");
    23        response.setHeader("Content-Disposition",
    24                "attachment; filename=" + FileDownloadUtils
    25                        .encodeDownloadFilename("工作单.pdf", ServletActionContext
    26                                .getRequest().getHeader("user-agent")));
    27        // 使用JRPdfExproter导出器导出pdf
    28        JRPdfExporter exporter = new JRPdfExporter();
    29        exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
    30        exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, ouputStream);
    31        exporter.exportReport();// 导出
    32        ouputStream.close();// 关闭流
    33  
    34        return NONE;
    35    }
    4、解决字体无法加载的异常
    将下载地址中的iTextAsian.jar部署到Maven本地仓库,命令 :
      mvn install:install-file -DgroupId=com.xxxxx -DartifactId=iTextAsain -Dversion=10.2.0.2.0 -Dpackaging=jar -Dfile=路径
    将pom文件中的坐标替换
    原坐标
    ​​<dependency>
      ​​​<groupId>com.itextpdf</groupId>
    ​​​  <artifactId>itext-asian</artifactId>
    ​​​  <version>5.2.0</version>
    ​​</dependency>
    新坐标,该坐标请根据自己本地仓库的实际情况进行修改
    ​​<dependency>
    ​​​  <groupId>com.alpha</groupId>
    ​​​  <artifactId>itextasain</artifactId>
    ​​​  <version>10.2.0.2.0</version>
    ​​</dependency>

    三、效果

    1、网页端

    2、生成的pdf文件:

  • 相关阅读:
    My first blog in cnblog
    浅析JavaScript中this储存
    input 文本框密码框的只读属性
    Js 数组——filter()、map()、some()、every()、forEach()、lastIndexOf()、indexOf()
    jquery使用$.getJson()跨域大数据量请求方法
    JS中关于clientWidth offsetWidth scrollWidth 等的含义及区别
    JS性能优化
    npm 创建 node.js 项目
    css 垂直居中的几种方法
    字符串转数组
  • 原文地址:https://www.cnblogs.com/gdwkong/p/8698095.html
Copyright © 2011-2022 走看看