润乾集算报表用Java动态修改报表数据源

    xiaoxiao2021-12-14  21

    实际应用中通过程序动态修改报表模板的情况很常见,其中动态修改数据源SQL就是一种典型场景。常见于系统中有一些结构相同而数据源不同的报表,为减少报表开发工作量,只开发一套报表模板,使用时通过程序动态修改数据源来满足实际需要。

      下面通过一个使用JAVA程序修改集算报表数据源SQL的例子说明使用过程。

    编辑报表模板:

    由于不同数据源的字段不同,因此这里使用动态表达式ds1.fname()获取字段名,ds1.field()获取字段值。此外,第一行和第一列为辅助行列,设置其隐藏。

     

    编写代码:

    1.读入报表

    String reportPath = request.getRealPath("/reportFiles/demo.rpx");

        ReportDefine rd = (ReportDefine)ReportUtils.read(reportPath);

     

    2.更改报表数据源

    DataSetMetaData dsmd=new DataSetMetaData();   //构造数据集元数据

        SQLDataSetConfig sdc=new SQLDataSetConfig();   //构造数据集定义

        sdc.setName("ds1");   //设置数据集名

        Stringsql = "";

        //根据不同参数,为报表设置不同数据源SQL,实际使用中可以从配置文件中读取

        switch(Integer.parseInt(type)){

           case 1:

               sql="select * from EMPLOYEE";

               break;

           case 2:

               sql="select * from PERFORMANCE";

               break;

           default:sql="select * from CUSTOMER";

        }

        sdc.setSQL(sql);    //设置 sql语句

        dsmd.addDataSetConfig(sdc);   //把数据集定义添加到数据集元数据

        rd.setDataSetMetaData(dsmd);   //把数据集元数据赋给ReportDefine

     

    3.      ReportDefine存入request后使用defineBean方式发布报表

        rd.setDataSetMetaData(dsmd);   //把数据集元数据赋给ReportDefine

        request.setAttribute("reportDefine",rd);

     

             <report:html name="report1"

               srcType="defineBean"

               beanName="reportDefine"

               exceptionPage="/reportJsp/jsp/myError.jsp"

      />

     

    实现效果:

      type=1时显示employee表数据:

     

       type=2时显示PERFORMANCE表数据:

       

    【附】changeds.jsp完整代码:

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 <%@ page contentType="text/html;charset=GBK"%> <%@ taglib uri="/WEB-INF/raqsoftReport.tld"prefix="report" %> <%@ page import="java.io.*"%> <%@ page import="java.util.*"%> <%@ page import="com.raqsoft.report.usermodel.Context"%> <%@ page import="com.raqsoft.report.model.ReportDefine"%> <%@ page import="com.raqsoft.report.util.ReportUtils"%> <%@page import="com.raqsoft.report.usermodel.SQLDataSetConfig"%> <%@page import="com.raqsoft.report.usermodel.DataSetMetaData"%>       <html> <link type="text/css"href="css/style.css" rel="stylesheet"/> <body topmargin=0 leftmargin=0 rightmargin=0 bottomMargin=0> <%     request.setCharacterEncoding("GBK");     String type = request.getParameter("type");     String reportPath = request.getRealPath("/WEB-INF/reportFiles/test.rpx");      ReportDefine rd = (ReportDefine)ReportUtils.read(reportPath);      DataSetMetaData dsmd=new DataSetMetaData();        SQLDataSetConfig sdc=new SQLDataSetConfig();        sdc.setName("ds1");        String sql = "";     switch(Integer.parseInt(type)){        case 1:            sql="select * from EMPLOYEE";            break;        case 2:            sql="select * from PERFORMANCE";            break;        default:sql="select * from CUSTOMER";     }     sdc.setSQL(sql);        dsmd.addDataSetConfig(sdc);        rd.setDataSetMetaData(dsmd);      String rptName = "RPT_"+Double.toString(Math.random());     request.setAttribute(rptName,rd);       %>   <jsp:include page="toolbar.jsp"flush="false" /> <table id="rpt"align="center" width=100% height=100%>     <tr><td align=center valign=top height=100%>        <report:html name="report1"             srcType="defineBean"             beanName="<%=rptName%>"   contextName="myContext"            exceptionPage="/reportJsp/jsp/myError.jsp"   />      </td></tr> </table>    </body> </html>
    转载请注明原文地址: https://ju.6miu.com/read-962179.html

    最新回复(0)