体检管理系统——JasperReports (PDF报表生成及Jaspersoft Studio使用 )

  1. 常见的PDF报表生成方式
    1.1 iText
    iText是著名的开放源码的站点sourceforge一个项目,是用于生成PDF文档的一个java类库。通过iText 不仅可以生成PDF或rtf的文档,而且可以将XML、Html文件转化为PDF文件。 iText的安装非常方便, 下载iText.jar文件后,只需要在系统的CLASSPATH中加入iText.jar的路径,在程序中就可以使用iText类 库了。

2.1 JasperReports快速体验
本小节我们先通过一个快速体验来感受一下JasperReports的开发过程。
第一步:创建maven工程,导入JasperReports的maven坐标
第二步:将提前准备好的jrxml文件复制到maven工程中(后面会详细讲解如何创建jrxml文件)
第三步:编写单元测试,输出PDF报表
@Test
public void test1() throws Exception{
String jrxmlPath =
“D:\itcastProject\health_parent\jasperReportsDemo\src\main\resources\demo.jrxml”;
String jasperPath =
“D:\itcastProject\health_parent\jasperReportsDemo\src\main\resources\demo.jasper”;

    //编译模板
    JasperCompileManager.compileReportToFile(jrxmlPath,jasperPath);

    //构造数据
    Map paramters = new HashMap();
    paramters.put("reportDate","2019-10-10");
    paramters.put("company","itcast");
    List<Map> list = new ArrayList();
    Map map1 = new HashMap();
    map1.put("name","xiaoming");
    map1.put("address","beijing");
    map1.put("email","[email protected]");
    Map map2 = new HashMap();
    map2.put("name","xiaoli");
    map2.put("address","nanjing");
    map2.put("email","[email protected]");
    list.add(map1);
    list.add(map2);

    //填充数据
    JasperPrint jasperPrint =
            JasperFillManager.fillReport(jasperPath,
                    paramters,
                    new JRBeanCollectionDataSource(list));

    //输出文件
    String pdfPath = "D:\\test.pdf";
    JasperExportManager.exportReportToPdfFile(jasperPrint,pdfPath);
}

2.2 JasperReports原理
在这里插入图片描述
JRXML:报表填充模板,本质是一个xml文件
Jasper:由JRXML模板编译成的二进制文件,用于代码填充数据
Jrprint:当用数据填充完Jasper后生成的对象,用于输出报表

2.3 开发流程
使用JasperReports导出pdf报表,开发流程如下:

  1. 制作报表模板
  2. 模板编译
  3. 构造数据
  4. 填充数据
  5. 输出文件

3.模板设计器Jaspersoft Studio

1,jasperreports入门案例
第一步:导入jar包的坐标
<dependency>
   <groupId>net.sf.jasperreports</groupId>
   <artifactId>jasperreports</artifactId>
   <version>6.8.0</version>
</dependency>
第二步:引入模板文件
 
第三步: 编写代码
@Test
   public void test1() throws Exception{
       String jrxmlPath = "D:\\workspace_idea\\itcast_health\\jasperReportsDemo\\src\\main\\resources\\demo.jrxml";
       String jasperPath =
               "D:\\workspace_idea\\itcast_health\\jasperReportsDemo\\src\\main\\resources\\demo.jasper";//编译模板
       JasperCompileManager.compileReportToFile(jrxmlPath,jasperPath);//构造数据
       Map paramters = new HashMap();
       paramters.put("reportDate","2019-10-10");
       paramters.put("company","itcast");
       
       
       
       List<Map> list = new ArrayList();
       Map map1 = new HashMap();
       map1.put("name","xiaoming");
       map1.put("address","beijing");
       map1.put("email","[email protected]");
       
       Map map2 = new HashMap();
       map2.put("name","xiaoli");
       map2.put("address","nanjing");
       map2.put("email","[email protected]");
       list.add(map1);
       list.add(map2);//填充数据
       JasperPrint jasperPrint =
               JasperFillManager.fillReport(jasperPath,
                       paramters,
                       new JRBeanCollectionDataSource(list));//输出文件
       String pdfPath = "D:\\robin\\test.pdf";
       JasperExportManager.exportReportToPdfFile(jasperPrint,pdfPath);
  }
API说明

JasperCompileManager : 编译模板
static void compileReportToFile(String sourceFileName, String destFileName)
​
JasperFillManager : 填充数据
   static JasperPrint fillReport(String sourceFileName, Map<String, Object> params)
   static JasperPrint fillReport(String sourceFileName, Map<String, Object> params,Connection connection)
  参数说明:
  sourceFileName :模板编译后文件的路径
  params : $P{}占位符的参数值填充。$F{}占位符的参数值从数据库中获取,我们不需要关注
   
   static JasperPrint fillReport(String sourceFileName, Map<String, Object> params, JRDataSource dataSource)
  参数说明:
  sourceFileName :模板编译后文件的路径
  $P{}占位符的参数值填充
  JRDataSource : 使用的是JRBeanCollectionDataSource类的对象
  封装list集合,而list集合中的数据是给$F{}占位符的参数赋值
​
JasperExportManager : 输出pdf文件的
static void exportReportToPdfFile(JasperPrint jasperPrint, String destFileName)
  destFileName : 最终生成pdf文件的路径
   static void exportReportToPdfStream(JasperPrint jasperPrint, OutputStream outputStream)
   
JasperPrint
发布了71 篇原创文章 · 获赞 1 · 访问量 1146

猜你喜欢

转载自blog.csdn.net/weixin_44993313/article/details/103841113
今日推荐