JAVA调用BIRT设置的SCRIPT数据集
数组方式:
第一步:定义一下JAVA,class(看后面附件)
第二步:定义一个LIST
第三步:在BIRT报表的DATASET事件中(open)中输入
count = 0;
cf = new Packages.user.ContactListFactory();
c = cf.createContactList();
在BIRT报表的DATASET事件中(CLOSE)中输入
cf=null; c=null;
在BIRT报表的DATASET事件中(FETCH)中输入
if (count <= c.length-1){
row["columnFirstName"] = c[count].getFname();
row["columnLastName"] = c[count].getLname();
row["columnPhoneNumber"]= c[count].getPhone();
count ++; return true; }
return false;
List方式
第一步:定义一下JAVA,class(看后面附件)
第二步:定义一个LIST
第三步:在BIRT报表的DATASET事件中(open)中输入
importPackage(.jshx);
count=0;
users=reslut;
在BIRT报表的DATASET事件中(CLOSE)中输入
cf=null; c=null;
在BIRT报表的DATASET事件中(FETCH)中输入
if (count<=users.size()){
row["columnFirstName"] = users.get(count).getFname();
row["columnLastName"] = users.get(count).getLname();
row["columnPhoneNumber"]= users.get(count).getPhone();
count++; return true; }
return false;
###############################################
附件
################################################
Contact类
package com.jshx;
public class Contact {
String fname;
String lname;
String phone;
public Contact(String fname, String lname, String phone){
this.fname = fname;
this.lname = lname;
this.phone = phone;
}
/*
*
* @return Returns the fname.
*/
public String getFname() {
return fname;
}
/**
* @param fname The fname to set.
*/
public void setFname(String fname) {
this.fname = fname;
}
/**
* @return Returns the lname.
*/
public String getLname() {
return lname;
}
/**
* @param lname The lname to set.
*/
public void setLname(String lname) {
this.lname = lname;
}
/**
* @return Returns the phone.
*/
public String getPhone() {
return phone;
}
/**
* @param phone The phone to set.
*/
public void setPhone(String phone) {
this.phone = phone;
}
}
ContactListFactory类
package com.jshx;
import java.util.ArrayList;
import java.util.List;
public class ContactListFactory {
public Contact[] createContactList(){
Contact[] c = new Contact[5];
c[0] = new Contact("stavros", "kounis", "2310886269");
c[1] = new Contact("dimitris", "kounis", "2310888270");
c[2] = new Contact("dimitris", "adamos", "2310998417");
c[3] = new Contact("nikos", "koufotolis", "2321013770");
c[4] = new Contact("yan", "liang", "13824745919");
return c;
}
public List createList(){
List c = new ArrayList();
c.add(new Contact("stavros", "kounis", "2310886269"));
c.add(new Contact("dimitris", "kounis", "2310888270"));
c.add(new Contact("dimitris", "adamos", "2310998417"));
c.add(new Contact("nikos", "koufotolis", "2321013770"));
c.add(new Contact("yan", "liang", "13824745919"));
return c;
}
}
ExecuteReport类
package com.jshx;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import org.eclipse.birt.core.framework.Platform;
import org.eclipse.birt.report.engine.api.EngineConfig;
import org.eclipse.birt.report.engine.api.EngineConstants;
import org.eclipse.birt.report.engine.api.EngineException;
import org.eclipse.birt.report.engine.api.HTMLRenderContext;
import org.eclipse.birt.report.engine.api.HTMLRenderOption;
import org.eclipse.birt.report.engine.api.IReportEngine;
import org.eclipse.birt.report.engine.api.IReportEngineFactory;
import org.eclipse.birt.report.engine.api.IReportRunnable;
import org.eclipse.birt.report.engine.api.IRunAndRenderTask;
import org.eclipse.birt.report.engine.api.ReportEngine;
public class ExecuteReport {
//报表
public static void executeReport() throws EngineException{
//Engine Configuration - set and get temp dir, BIRT home, Servlet context
EngineConfig config = new EngineConfig();
config.setEngineHome("D:/soft/Tomcat6/virtualdir/birt/runtime/ReportEngine");
//Create the report engine
ReportEngine engine = new ReportEngine(config);
//Open a report design - use design to modify design, retrieve embedded images etc.
IReportRunnable design = engine.openReportDesign("D:/soft/Tomcat6/virtualdir/birt/report/kenScripDataset.rptdesign");
//Create task to run the report - use the task to execute and run the report,
IRunAndRenderTask task = engine.createRunAndRenderTask(design);
//Set Render context to handle url and image locataions
HTMLRenderContext renderContext = new HTMLRenderContext();
renderContext.setImageDirectory("image");
HashMap contextMap = new HashMap();
contextMap.put( EngineConstants.APPCONTEXT_HTML_RENDER_CONTEXT, renderContext);
task.setAppContext( contextMap );
//传数组
/*ContactListFactory cf = new ContactListFactory();
Contact[] c = cf.createContactList();
task.addScriptableJavaObject("reslut", c);*/
//传LIST
ContactListFactory cf = new ContactListFactory();
List c = cf.createList();
task.addScriptableJavaObject("reslut", c);
//Set rendering options - such as file or stream output,
//output format, whether it is embeddable, etc
HTMLRenderOption options = new HTMLRenderOption();
options.setOutputFileName("D:/soft/Tomcat6/virtualdir/birt/report/kenScripDataset.html");
options.setOutputFormat("html");
task.setRenderOption(options);
//run the report and destroy the engine
task.run();
engine.destroy();
}
}
Reporttest类
public class Reporttest {
public static void main(String args[]) {
try {
ExecuteReport mreport = new ExecuteReport();
mreport.executeReport();
}catch ( Exception e ) {
e.printStackTrace();
}
}
}