iReport+JasperReport报表开发

在JAVA SWING项目中需要用到报表打印 

1.下载iReport

2.安装-新建

3.下面是我的报表DEMO

jrxml

4.报表格式


5.使用JavaBean作为数据源

   数据-数据源连接-添加-JavaBean数据源-下一步-输入对应的javabean类名称(包括包名)-测试-连接成功即可

6.选择数据库 如图


7.在弹出的报表查询中--JavaBean数据源

   类名中输入刚才输入的类名-读取属性--确定

8.即可回到4中,将字段拖到报表中。

9.项目中


private JRViewer jrview = null;

private JasperPrint jasperPrint;
    private JasperReport report;


JRDataSource ds = new JRBeanCollectionDataSource(list);  list即JAVABEAN的数据列表

String reportFilePath = this.getClass()
                    .getResource("fkebean.jasper").getPath();
            // 生成JasperPrint
            report = (JasperReport) JRLoader.loadObject(reportFilePath);
            //  用参数传递过去的 hm
            jasperPrint = JasperFillManager.fillReport(report, hm, ds);//hm是hashMap用力传递参数

          jrview = new JRViewer(jasperPrint);

panel.add(BorderLayout.CENTER, jrview);

10.打印

// 执行打印操作
            JasperPrintManager.printReport(jasperPrint, false);//false表示不显示打印设置窗口


============如果用JDBC作为数据源的话,那么====================

 Connection con;
             // 加载报表模版
             InputStream inReport = PrintFrame.class
             .getResourceAsStream("fke2.jrxml");
             jasperReport = JasperCompileManager.compileReport(inReport);
             // 建立连接
             con = ConnectionManager.getConnection();

/jasperPrint = JasperFillManager.fillReport(filePath, hm, ds);
            // 在使用iReport设计了报表之后可以使用连接对象直接填充报表。
            jasperPrint = JasperFillManager.fillReport(jasperReport, hm, con);
             // 打印预览
            JasperViewer.viewReport(jasperPrint);

            // 打印
            JasperPrintManager.printReport(jasperPrint, false);


 // 关闭资源
            ConnectionManager.close(con);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值