Eclipse RCP入门(六)
集成了BIRT2.2.2后,想要把一个list数据放置到报表的模板中,
需要使用到birt script
显示报表的SampleView.java做了如下修改:
private void previewReport() throws EngineException {
EngineConfig config = new EngineConfig();
config.setLogConfig("D:/birt/logs", Level.FINE);
// Create the report engine
IReportEngineFactory factory = (IReportEngineFactory) Platform
.createFactoryObject( IReportEngineFactory.EXTENSION_REPORT_ENGINE_FACTORY );
IReportEngine engine = factory.createReportEngine( config );
IReportRunnable design = null;
try {
String report = filePathTxt.getText();
FileInputStream fs = new FileInputStream(report);
design = engine.openReportDesign(fs);
IRunAndRenderTask task = engine.createRunAndRenderTask(design);
//加入数据
task.setParameterValue("sample", "Carl");
List list = new ArrayList();
User user1 = new User("1","sillycat");
User user2 = new User("2","kiko");
list.add(user1);
list.add(user2);
BirtDataSourceObject birtDataSourceObject = new BirtDataSourceObject();
birtDataSourceObject.setResultList(list);
task.addScriptableJavaObject ("dsFactory", birtDataSourceObject);
IRenderOption htmlOptions;
htmlOptions = new HTMLRenderOption( );
ByteArrayOutputStream bos = new ByteArrayOutputStream();
htmlOptions.setOutputStream(bos);
htmlOptions.setOutputFormat(HTMLRenderOption.OUTPUT_FORMAT_HTML);
task.setRenderOption(htmlOptions);
// run the report and destroy the engine
task.run();
task.close();
//set Browser text accordingly
browser.setText(bos.toString());
engine.destroy();
} catch (Exception e) {
e.printStackTrace();
}
}
其中的task.addScriptableJavaObject ("dsFactory", birtDataSourceObject);
就是加入了一个ArrayList,名字定义为dsFactory
然后打开test.rptdesign模板,在Data Sources中加入一个Scripted data source名字叫dSource
再在Data Sets中加入一个dSet,在OutPut Column里面加入UserId和UserName
然后选中dSet,birt的编辑器里面不要显示layout,而是切换到显示Script这里。
选择open在里面加入如下脚本:
rtn = dsFactory.getResultList();
totalrows = rtn.size();
currentrow = 0;
在fetch中加入如下脚本:
if(currentrow >= totalrows){
return false;
}
var favrow = rtn.get(currentrow);
var userId = favrow.getUserId();
var userName = favrow.getUserName();
row["userId"] = userId;
row["userName"] = userName;
currentrow = currentrow + 1;
return true;
在close中加入如下脚本:
rtn = null;
其中JAVA还有一些类,清单如下,
类 BirtDataSourceObject.java:
package com.sillycat.models;
import java.util.ArrayList;
import java.util.List;
public class BirtDataSourceObject {
private List resultList = new ArrayList();
public List getResultList() {
return resultList;
}
public void setResultList(List resultList) {
this.resultList = resultList;
}
}
用户的DO类 User.java:
package com.sillycat.models;
public class User {
private String userId;
private String userName;
public User(){
}
public User(String userId,String userName){
this.userId = userId;
this.userName = userName;
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
}
集成了BIRT2.2.2后,想要把一个list数据放置到报表的模板中,
需要使用到birt script
显示报表的SampleView.java做了如下修改:
private void previewReport() throws EngineException {
EngineConfig config = new EngineConfig();
config.setLogConfig("D:/birt/logs", Level.FINE);
// Create the report engine
IReportEngineFactory factory = (IReportEngineFactory) Platform
.createFactoryObject( IReportEngineFactory.EXTENSION_REPORT_ENGINE_FACTORY );
IReportEngine engine = factory.createReportEngine( config );
IReportRunnable design = null;
try {
String report = filePathTxt.getText();
FileInputStream fs = new FileInputStream(report);
design = engine.openReportDesign(fs);
IRunAndRenderTask task = engine.createRunAndRenderTask(design);
//加入数据
task.setParameterValue("sample", "Carl");
List list = new ArrayList();
User user1 = new User("1","sillycat");
User user2 = new User("2","kiko");
list.add(user1);
list.add(user2);
BirtDataSourceObject birtDataSourceObject = new BirtDataSourceObject();
birtDataSourceObject.setResultList(list);
task.addScriptableJavaObject ("dsFactory", birtDataSourceObject);
IRenderOption htmlOptions;
htmlOptions = new HTMLRenderOption( );
ByteArrayOutputStream bos = new ByteArrayOutputStream();
htmlOptions.setOutputStream(bos);
htmlOptions.setOutputFormat(HTMLRenderOption.OUTPUT_FORMAT_HTML);
task.setRenderOption(htmlOptions);
// run the report and destroy the engine
task.run();
task.close();
//set Browser text accordingly
browser.setText(bos.toString());
engine.destroy();
} catch (Exception e) {
e.printStackTrace();
}
}
其中的task.addScriptableJavaObject ("dsFactory", birtDataSourceObject);
就是加入了一个ArrayList,名字定义为dsFactory
然后打开test.rptdesign模板,在Data Sources中加入一个Scripted data source名字叫dSource
再在Data Sets中加入一个dSet,在OutPut Column里面加入UserId和UserName
然后选中dSet,birt的编辑器里面不要显示layout,而是切换到显示Script这里。
选择open在里面加入如下脚本:
rtn = dsFactory.getResultList();
totalrows = rtn.size();
currentrow = 0;
在fetch中加入如下脚本:
if(currentrow >= totalrows){
return false;
}
var favrow = rtn.get(currentrow);
var userId = favrow.getUserId();
var userName = favrow.getUserName();
row["userId"] = userId;
row["userName"] = userName;
currentrow = currentrow + 1;
return true;
在close中加入如下脚本:
rtn = null;
其中JAVA还有一些类,清单如下,
类 BirtDataSourceObject.java:
package com.sillycat.models;
import java.util.ArrayList;
import java.util.List;
public class BirtDataSourceObject {
private List resultList = new ArrayList();
public List getResultList() {
return resultList;
}
public void setResultList(List resultList) {
this.resultList = resultList;
}
}
用户的DO类 User.java:
package com.sillycat.models;
public class User {
private String userId;
private String userName;
public User(){
}
public User(String userId,String userName){
this.userId = userId;
this.userName = userName;
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
}