叽哩咕噜

君子如玉,上善若水

从零开始认识 JasperReport + IReport (JasperReport部分)

因参与公司南非的项目,需要应用在Linux平台,所以报表改用JasperReport.国内的项目报表还是用FastReport(看来还是结晶啊).

废话少说,我从delphi平台转到RCP开发才不过两月时间,以前从未接触过JasperReport.(简称为JR)
今天上级要求熟悉JR,为节后国际化报表做准备.以下为我从0开始的一些记录.
目的: 力求理解JasterReport的各个概念及之间的关系.熟悉打印报表流程的来龙去脉,主要的调用方法(如加载报表,打印,预览).       
        报表设计器的使用.

1. IReport 设计器,就像FastReport一样有这样的设计器.但名字叫IReprot,为安装文件,我安装的版本为
    iReport-3.0.0-windows-installer.exe,同事传我的,下载地址百度下下就有了.
2. JasperReport为一个开发Jar包,就是相当于FastReport的报表控件.
3. 初始化JasterReport
private static JasperPrint initJasperReport(String fileName,
            Map<String, Object> paramMap, List data) throws IOException,
            MalformedURLException, JRException {
        ...
        InputStream is = fullPathString.toURL().openStream();  //这句以上代码都是处理报表文件路径
        JasperReport jasperReport = (JasperReport) JRLoader.loadObject(is);       
        Object[] objArray = data.toArray();
        //JasperReport对象  + Map对象 + List 对象 =JasperPrint对象
        final JasperPrint jasperPrint = JasperFillManager.fillReport(
                jasperReport, paramMap, new JRBeanArrayDataSource(objArray));
        return jasperPrint;
    }

4.查找默认打印机(打印服务).
public static boolean directPrintByPrintName(final JasperPrint jasperPrint) {
        if (jasperPrint != null) {
            try {
                PrintService[] PSs = PrinterJob.lookupPrintServices(); //java.awt.*包.查找所有打印服务.
                PrintService ps = null;
                if (PSs != null && PSs.length > 1&& !Assert.isNull(MzTransParam.PrinterOfSyddyj)) {
                    for (int i = 0; i < PSs.length; i++) {
                        String sps = PSs[i].toString();
                        sps = sps.replace("Win32 Printer : ", ""); //$NON-NLS-1$ //$NON-NLS-2$
                        //MzTransParam.PrinterOfSyddyj 我们系统设置的默认打印机名称.
                        if (sps.equalsIgnoreCase(MzTransParam.PrinterOfSyddyj)) { 
                            ps = PSs[i];//得到打印服务对象
                            break;
                        }
                    }
                }
5.设置打印参数,好多个参数
                if (ps != null) {
                    long start = System.currentTimeMillis();
                    PrintRequestAttributeSet printRequestAttributeSet = new HashPrintRequestAttributeSet();
                    printRequestAttributeSet.add(MediaSizeName.ISO_A5);// 处方模板是A5纸  第一个参数对象   

                    PrintServiceAttributeSet printServiceAttributeSet = new HashPrintServiceAttributeSet();
                    printServiceAttributeSet.add(new PrinterName(ps.getName(),null)); //第二个参数对象

                    final JRPrintServiceExporter exporter = new JRPrintServiceExporter(); //关键的对象,其它的对象都是为他服务的
                    //以下为设置参数
                    exporter.setParameter(JRExporterParameter.JASPER_PRINT,jasperPrint);
                    exporter.setParameter(JRPrintServiceExporterParameter.PRINT_REQUEST_ATTRIBUTE_SET,
                                    printRequestAttributeSet);
                    exporter.setParameter(JRPrintServiceExporterParameter.PRINT_SERVICE_ATTRIBUTE_SET,
                                    printServiceAttributeSet);
                    exporter.setParameter(JRPrintServiceExporterParameter.DISPLAY_PAGE_DIALOG,
                                    Boolean.FALSE);
                    exporter.setParameter(JRPrintServiceExporterParameter.DISPLAY_PRINT_DIALOG,
                                    Boolean.FALSE);
6.关键的出场,在线程里导出报表.(打印)
                Thread thread = new Thread(new Runnable() {
                        public void run() {
                            try {
                                exporter.exportReport(); //就这么一句.exporter对象导出报表.
                            } catch (Exception ex) {
                                System.err.println(ex.getLocalizedMessage());
                            }
                        }
                    });
                    thread.start();
7.采用默认打印.
                } else { //此处的else接的是5条的if
                    Thread thread = new Thread(new Runnable() {
                        public void run() {
                            try {
                                //jasperPrint 对象就是JasperPrintManager生成的.参考上面的代码.
                                JasperPrintManager.printReport(jasperPrint,false);  //这一句应该是默认打印.
                            } catch (Exception ex) {
                            }
                        }
                    });
                    thread.start();
                }
            } catch (Exception ex) {
                return false;
            }

        }
        return true;}
未完.


   

posted on 2011-09-09 07:49 叽哩咕噜 阅读(3956) 评论(3)  编辑  收藏

评论

# re: 从零开始认识 JasperReport + IReport[未登录] 2011-09-09 08:08 kevin

建议你使用ireport 4.1.1功能上比3.0要强大得多。  回复  更多评论   

# re: 从零开始认识 JasperReport + IReport (JasperReport部分) 2011-09-11 04:36 wangxunhua

@kevin
感谢Kevin兄指点,因我现在的主要的工作不参与开发,只参与报表设计,但现在对jasperreport 还没有一个整体的概念.3.0,4.1.1强大之处自是无从谈起.不知可不可以指点下3.0 和4.1.1的区别.4.1.1的新特性.或是推荐一下文章.  回复  更多评论   

# re: 从零开始认识 JasperReport + IReport (JasperReport部分)[未登录] 2011-09-13 08:37 kevin

@wangxunhua
4.1.1的话,支持新的html组件以及google map,以及修正了若干的导出报表excel 和word的bug,采用了新的生成器。目前最新的版本是4.1.2,开发的话,使用3.7的这个版本的应该很多。供您参考。谢谢。  回复  更多评论   


只有注册用户登录后才能发表评论。


网站导航:
 

公告

疯狂

导航

<2011年9月>
28293031123
45678910
11121314151617
18192021222324
2526272829301
2345678

统计

常用链接

留言簿

随笔分类(17)

随笔档案(22)

文章分类(1)

文章档案(1)

工程教程

牛人BLOG

搜索

最新评论

阅读排行榜

评论排行榜