標籤:集算報表 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動態修改報告資料來源