It is common for applications to dynamically modify report templates through a program, in which the data source is dynamically modified SQL is a typical scenario. Common in the system have some of the same structure and different data sources of the report, in order to reduce the report development workload, only a set of report templates, so that time through the program dynamically modify the data source to meet the actual needs.
below through a use The JAVA program modifies the collection report data source SQL Example to illustrate the use process.
To edit a report template:
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 "/>
because the fields of different data sources are different, we get the field names using the dynamic expression ds1.fname () , andDs1.field () Gets the field values. In addition, the first row and first column are listed as secondary columns, which are set to hide.
Write code:
1. read in the report
Stringreportpath = Request. Getrealpath ( "/reportfiles/demo.rpx") ;
Reportdefinerd = (reportdefine) reportutils.read (Reportpath);
2. change the report data source
DataSetMeta datadsmd= new datasetmetadata (); // constructing dataset metadata
sqldatasetconfigsdc= New Sqldatasetconfig (); // construct a dataset definition
sdc.setname ("DS1"); // Set data set name
Stringsql = "" ;
// based on different parameters, set different data source SQL for the report , can be read from the configuration file in actual use
Switch(integer.parseint (type)) {
case 1:
sql= the SELECT * from Employee Table " ;
break;
case 2:
sql= the SELECT * from Order Details Order by Order ID " ;
break;
default: sql="select * from Customer Sales form ";
}
sdc.setsql (SQL); // set SQL statements
Dsmd.adddatasetconfig (SDC); // Add the dataset definition to the dataset metadata
Rd.setdatasetmetadata (DSMD); // Assign data set metadata to reportdefine
3. Publish the report using the Definebean method after depositing the reportdefine to request
Rd.setdatasetmetadata (DSMD); // Assign data set metadata to reportdefine
Request.setattribute ("Reportdefine", RD);
< report:html Name="Report1"
srctype="Definebean"
beanname="Reportdefine"
exceptionpage="/reportjsp/jsp/myerror.jsp"
/>
Implementation results:
Show employee Information table data when 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 "/>
Show Order schedule data when 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 "/>
"attached"changeds.jsp Full code:
<%@ 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 "%>
This article is from the High performance report data calculation blog, so be sure to keep this source http://report5.blog.51cto.com/8028595/1633839
The report data source is dynamically modified by the collection report in Java