集算報表用Java動態修改報告資料來源

來源:互聯網
上載者:User

標籤:集算報表   java   動態修改資料來源   報表資料來源   

  實際應用中通過程式動態修改報告模板的情況很常見,其中動態修改資料來源SQL就是一種典型情境。常見於系統中有一些結構相同而資料來源不同的報表,為減少報表開發工作量,只開發一套報表範本,使用時通過程式動態修改資料來源來滿足實際需要。

  下面通過一個使用JAVA程式修改集算報表資料來源SQL的例子說明使用過程。

編輯報表範本:

650) this.width=650;" src="http://s3.51cto.com/wyfs02/M01/6B/8B/wKioL1UwnlGwjbZCAABVo_syq58812.jpg" style="float:none;" title="report5_application_modify_datasource_1.jpg" alt="wKioL1UwnlGwjbZCAABVo_syq58812.jpg" />

由於不同資料來源的欄位不同,因此這裡使用動態運算式ds1.fname()擷取欄位名,ds1.field()擷取欄位值。此外,第一行和第一列為輔助行列,設定其隱藏。

 

編寫代碼:

1.讀入報表

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

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

 

2.更改報表資料來源

DataSetMetaDatadsmd=newDataSetMetaData();   //構造資料集中繼資料

    SQLDataSetConfigsdc=newSQLDataSetConfig();   //構造資料集定義

    sdc.setName("ds1");   //設定資料集名

    Stringsql = "";

    //根據不同參數,為報表設定不同資料來源SQL,實際使用中可以從設定檔中讀取

    switch(Integer.parseInt(type)){

       case 1:

           sql="select * from 員工表";

           break;

       case 2:

           sql="select * from 訂單明細 order by 訂單ID";

           break;

       default:sql="select * from 客戶銷售表";

    }

    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時顯示員工資訊表資料:

650) this.width=650;" src="http://s3.51cto.com/wyfs02/M01/6B/8F/wKiom1UwnPzi7mbpAAGIhEWIFqE226.jpg" style="float:none;" title="report5_application_modify_datasource_2.jpg" alt="wKiom1UwnPzi7mbpAAGIhEWIFqE226.jpg" />

   當type=2時顯示訂單明細表資料:

650) this.width=650;" src="http://s3.51cto.com/wyfs02/M02/6B/8B/wKioL1UwnlKRVh-4AAFGuA_0ga8129.jpg" style="float:none;" title="report5_application_modify_datasource_3.jpg" alt="wKioL1UwnlKRVh-4AAFGuA_0ga8129.jpg" />


【附】changeds.jsp完整代碼:

<%@ 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");    Stringtype = request.getParameter("type");    //取得報表真實路徑    StringreportPath = request.getRealPath("/reportFiles/demo.rpx");     ReportDefinerd = (ReportDefine)ReportUtils.read(reportPath);     DataSetMetaDatadsmd=newDataSetMetaData();   //構造資料集中繼資料    SQLDataSetConfigsdc=newSQLDataSetConfig();   //構造資料集定義    sdc.setName("ds1");   //設定資料集名    Stringsql = "";    //根據不同參數,為報表設定不同資料來源SQL,實際使用中可以從設定檔中讀取    switch(Integer.parseInt(type)){       case 1:           sql="select * from 員工表";           break;       case 2:           sql="select * from 訂單明細 order by 訂單ID";           break;       default:sql="select * from 客戶銷售表";    }    sdc.setSQL(sql);    //設定 sql語句    dsmd.addDataSetConfig(sdc);   //把資料集定義添加到資料集中繼資料    rd.setDataSetMetaData(dsmd);   //把資料集中繼資料賦給ReportDefine     request.setAttribute("reportDefine",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="reportDefine"           exceptionPage="/reportJsp/jsp/myError.jsp"  />     </td></tr></table> </body></html>


本文出自 “高效能報表資料計算” 部落格,請務必保留此出處http://report5.blog.51cto.com/8028595/1633839

集算報表用Java動態修改報告資料來源

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.