集算报表用Java动态修改报表数据源
? 實(shí)際應(yīng)用中通過(guò)程序動(dòng)態(tài)修改報(bào)表模板的情況很常見(jiàn),其中動(dòng)態(tài)修改數(shù)據(jù)源SQL就是一種典型場(chǎng)景。常見(jiàn)于系統(tǒng)中有一些結(jié)構(gòu)相同而數(shù)據(jù)源不同的報(bào)表,為減少報(bào)表開(kāi)發(fā)工作量,只開(kāi)發(fā)一套報(bào)表模板,使用時(shí)通過(guò)程序動(dòng)態(tài)修改數(shù)據(jù)源來(lái)滿(mǎn)足實(shí)際需要。
??下面通過(guò)一個(gè)使用JAVA程序修改集算報(bào)表數(shù)據(jù)源SQL的例子說(shuō)明使用過(guò)程。
編輯報(bào)表模板:
由于不同數(shù)據(jù)源的字段不同,因此這里使用動(dòng)態(tài)表達(dá)式ds1.fname()獲取字段名,ds1.field()獲取字段值。此外,第一行和第一列為輔助行列,設(shè)置其隱藏。
?
編寫(xiě)代碼:
1.讀入報(bào)表
StringreportPath = request.getRealPath("/reportFiles/demo.rpx");
??? ReportDefinerd = (ReportDefine)ReportUtils.read(reportPath);
?
2.更改報(bào)表數(shù)據(jù)源
DataSetMetaDatadsmd=newDataSetMetaData();?? //構(gòu)造數(shù)據(jù)集元數(shù)據(jù)
??? SQLDataSetConfigsdc=newSQLDataSetConfig();?? //構(gòu)造數(shù)據(jù)集定義
??? sdc.setName("ds1");?? //設(shè)置數(shù)據(jù)集名
??? Stringsql = "";
??? //根據(jù)不同參數(shù),為報(bào)表設(shè)置不同數(shù)據(jù)源SQL,實(shí)際使用中可以從配置文件中讀取
??? switch(Integer.parseInt(type)){
?????? case 1:
?????????? sql="select * from 員工表";
?????? ??? break;
?????? case 2:
?????????? sql="select * from 訂單明細(xì) order by 訂單ID";
?????????? break;
?????? default:sql="select * from 客戶(hù)銷(xiāo)售表";
??? }
??? sdc.setSQL(sql);??? //設(shè)置 sql語(yǔ)句
??? dsmd.addDataSetConfig(sdc);?? //把數(shù)據(jù)集定義添加到數(shù)據(jù)集元數(shù)據(jù)
??? rd.setDataSetMetaData(dsmd);?? //把數(shù)據(jù)集元數(shù)據(jù)賦給ReportDefine
?
3.??????將ReportDefine存入request后使用defineBean方式發(fā)布報(bào)表
??? rd.setDataSetMetaData(dsmd);?? //把數(shù)據(jù)集元數(shù)據(jù)賦給ReportDefine
??? request.setAttribute("reportDefine",rd);
?
???????? <report:html name="report1"
??? ?????? srcType="defineBean"
??? ?????? beanName="reportDefine"
??? ?????? exceptionPage="/reportJsp/jsp/myError.jsp"
? />
?
實(shí)現(xiàn)效果:
??當(dāng)type=1時(shí)顯示員工信息表數(shù)據(jù):
? ?當(dāng)type=2時(shí)顯示訂單明細(xì)表數(shù)據(jù):
【附】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");//取得報(bào)表真實(shí)路徑StringreportPath?=?request.getRealPath("/reportFiles/demo.rpx");?ReportDefinerd?=?(ReportDefine)ReportUtils.read(reportPath);?DataSetMetaDatadsmd=newDataSetMetaData();???//構(gòu)造數(shù)據(jù)集元數(shù)據(jù)SQLDataSetConfigsdc=newSQLDataSetConfig();???//構(gòu)造數(shù)據(jù)集定義sdc.setName("ds1");???//設(shè)置數(shù)據(jù)集名Stringsql?=?"";//根據(jù)不同參數(shù),為報(bào)表設(shè)置不同數(shù)據(jù)源SQL,實(shí)際使用中可以從配置文件中讀取switch(Integer.parseInt(type)){case?1:sql="select?*?from?員工表";break;case?2:sql="select?*?from?訂單明細(xì)?order?by?訂單ID";break;default:sql="select?*?from?客戶(hù)銷(xiāo)售表";}sdc.setSQL(sql);????//設(shè)置?sql語(yǔ)句dsmd.addDataSetConfig(sdc);???//把數(shù)據(jù)集定義添加到數(shù)據(jù)集元數(shù)據(jù)rd.setDataSetMetaData(dsmd);???//把數(shù)據(jù)集元數(shù)據(jù)賦給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>轉(zhuǎn)載于:https://blog.51cto.com/report5/1633839
總結(jié)
以上是生活随笔為你收集整理的集算报表用Java动态修改报表数据源的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: java 从网络Url中下载文件
- 下一篇: Javascript创建类的七种方法