自定义菜单实现按客户要求新建报表

目前越来越多的终端业务人员在进行报表设计工作,他们希望在新建的报表模板能否按照我自己定义的业务规则来生成,这样直接基于该模板设计报表能够大大提升报表设计效率,简单来说能否新建的报表模板就是另一张报表的展现结果,但是此时不能用打开方式,因为这个模板里边的科目要根据数据库中的数据自动变化,比如现在数据库中有10条记录,我新建时就生成10行的模板,如果数据库数据变为12条则新建的模板也动态变成12条,并且提供保存和再打开功能。

解决方法:

润乾报表提供接口支持自定义设计器菜单,所以可以自定义菜单实现相应功能,自己实现新建报表功能,在程序中读取数据库中的数据,然后动态将值插入到报表中再返回给设计器。

一:增加设计器菜单

用文本编辑器打开 设计器根目录/designer\config下的systemconfig.xml,找到<CONFIG_MENU>

</CONFIG_MENU>

在此中间加入

<F_20001 argument=”"  classname=”createRaq” text=”新建报表“/>

<F_20002 argument=”E:/”  classname=”saveRaq” text=”保存报表“/>

<F_20100 argument=”E:/”  classname=”openRaq” text=”打开报表“/>

Argument为程序传入参数,可以在此向程序中传递参数,classname为点击时调用的classtext为显示菜单名称。

注:在润乾帮助文档中介绍配置文件为report4config.xml,但经验证V4.5版本配置文件要修改systemconfig.xml才能生效。

二:实现菜单程序

新建:

              ReportDefine rd = new ReportDefine2(10, 10);//新生成1010列报表

              int i=1;

              try {

                     //连接数据库,正式使用改成自己的数据库配置

                     Class.forName(“org.hsqldb.jdbcDriver”).newInstance();

                     Connection   conn=   DriverManager.getConnection(“jdbc:hsqldb:hsql://localhost/runqianDB”,”sa”,”");        

                     Statement   stmt=conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY);        

                     String   sql=”select distinct 地区 from 客户“;  //从数据表中取科目,这里取地区

                     ResultSet rs=stmt.executeQuery(sql);

                     while(rs.next()){

                            INormalCell icell_11 = rd.getCell(i, (short) 1);

                            icell_11.setValue(rs.getString(1));//将从数据库中取出数据放到单元格中

                            i++;

                            }

                    

              } catch (InstantiationException e2) {

                     // TODO 自动生成 catch

                     e2.printStackTrace();

              } catch (IllegalAccessException e2) {

                     // TODO 自动生成 catch

                     e2.printStackTrace();

              } catch (ClassNotFoundException e2) {

                     // TODO 自动生成 catch

                     e2.printStackTrace();

              } catch (SQLException e1) {

                     // TODO 自动生成 catch

                     e1.printStackTrace();

              }       

             

              OutputStream out =null;

              ByteArrayOutputStream os = new ByteArrayOutputStream();

              try {

                     ReportUtils.write(os,rd);

              } catch (Exception e1) {

                     // TODO 自动生成 catch

                     e1.printStackTrace();

              }

      

                InputStream is = new ByteArrayInputStream(os.toByteArray());

                            HashMap ht=new HashMap();

                            ht.put(“stream”, is);

                            ht.put(“filename”, raqFile);

                            handler.processMessage( “setRaqInputStream”, ht );//打开报表

 

保存:

String filePath=argument+”/”+raqFile;//argumeng可以接收配置文件传递过来的参数,此处传递E盘,将来可传递服务器地址,更改保存路径代码即可

    System.out.println(“filepath==”+filePath);

    OutputStream out;

       try {

              out = new FileOutputStream( filePath);

              ReportUtils.write(out,report);

       } catch (FileNotFoundException e1) {

              // TODO 自动生成 catch

              e1.printStackTrace();

       } catch (Exception e1) {

              // TODO 自动生成 catch

              e1.printStackTrace();

       }

 

打开:

int iTmp = 0;

                     if( (iTmp = raqFile.lastIndexOf(“.raq”)) <= 0 ){

                            raqFile = raqFile + “.raq”;

                            iTmp = 0;

                     }

                        String filePath=argument+”/”+raqFile;

                        System.out.println(“filePath====”+filePath);

                        File raq=new File(filePath);

                        try {

                            InputStream ins = new FileInputStream(raq);

                            HashMap hm = new HashMap();

                            hm.put( “stream”, ins );

                            hm.put( “filename”, filePath );

                            handler.processMessage( “httpopen”, hm );

                     } catch (FileNotFoundException e1) {

                            // TODO 自动生成 catch

                            e1.printStackTrace();

                     };

 

这样就能实现新建报表按照业务需求生成相应报表并提供保存和打开功能,如图:

菜单:

新建后报表: