JAVA调用BIRT设置的SCRIPT数据集

把编译好的类,连同包路径,一起放到Eclipse的Birt插件下面
比如我的就是在
c:\software\Birt2.6.2\plugins\org.eclipse.birt.report.viewer_2.6.2.r262_v20110214\birt\WEB-INF\classes\SimpleClass.class,
当然你的类如果带有包,比如com.jshx.SimpleClass, 那么你必须把整个的com包都放到classes下面

数组方式:
第一步:定义一下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(Packages.com.jshx);
reslut=reslutList;
count=0;


在BIRT报表的DATASET事件中(CLOSE)中输入
reslut=null; 


在BIRT报表的DATASET事件中(FETCH)中输入

if (count<=reslut.size()){
row["columnFirstName"] = reslut.get(count).getFname();
row["columnLastName"] = reslut.get(count).getLname();
row["columnPhoneNumber"]= reslut.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<Contact> createList(){
List<Contact> c = new ArrayList<Contact>();
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;
}
}


公共方法类
CommonFunc.java


package com.jshx.util;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


public class CommonFunc {
private static final Log logger = LogFactory.getLog(CommonFunc.class);
/**
* 获取当前项目的绝对路径
* @param classesPath
* @return
*/
public static String getPorjectPath(String classesPath){
String tempdir;
String classPath[] = classesPath.split("webapps");
tempdir=classPath[0];
if(!"/".equals(tempdir.substring(tempdir.length()-1))){
tempdir += "/";
}
return tempdir;
}

/**
* 日期转为字符串
* @param date
* @param formatStr
* @return
*/
public static String ConvertDateToStr(Date date,String formatStr)
{
try {
SimpleDateFormat format = new SimpleDateFormat(formatStr);
return format.format(date);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}



Constants.java


package com.jshx.util;

/**
* 常量类 <功能详细描述>
* @author duanpf
* @version [版本号, 2012-12-8]
* @see [相关类/方法]
* @since [产品/模块版本]
*/

public class Constants{
// Field descriptor #6 Ljava/lang/String;
public static final String YYYY_MM_DD_HH_MM = "yyyy-MM-dd HH:mm";

// Field descriptor #6 Ljava/lang/String;
public static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";

// Field descriptor #6 Ljava/lang/String;
public static final String YYYY_MM_DD = "yyyy-MM-dd";

// Field descriptor #6 Ljava/lang/String;
public static final String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";

public static final String YYYYMM = "yyyyMM";

public static final String YYYY = "yyyy";

public static final String MM = "MM";

public static final String birtRunTime = "virtualdir/birt/runtime/ReportEngine";

public static final String birtPath = "virtualdir/birt/report/";
}


ExecuteReport类


package com.jshx;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.log4j.Logger;
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.IReportRunnable;
import org.eclipse.birt.report.engine.api.IRunAndRenderTask;
import org.eclipse.birt.report.engine.api.ReportEngine;

import com.jshx.util.CommonFunc;
import com.jshx.util.Constants;

public class ExecuteReport {
public static Logger logger = Logger.getLogger(ExecuteReport.class);

/**
*
* @param paraMap 传入需要在报表中展示的值,或LIST集合
* @param rptdesign 报表模板
* @param outFlie 输出的文件名
* @param outType 输出的文件类型
* @return
* @throws EngineException
*/
public String executeReport(Map<String, Object> paraMap,String rptdesign,String outFlie,String outType) throws EngineException{
String classesPath = this.getClass().getClassLoader().getResource("").getPath();
String realPath= CommonFunc.getPorjectPath(classesPath);

//Engine Configuration - set and get temp dir, BIRT home, Servlet context
EngineConfig config = new EngineConfig();
logger.info("运行时路径="+realPath+ Constants.birtRunTime);
config.setEngineHome(realPath+ Constants.birtRunTime);

//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(realPath+ Constants.birtPath +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 );

if(null!=paraMap&&!paraMap.isEmpty()){
Set<String> set = paraMap.keySet();
for (String string : set) {
if("reslut".equals(string)){
Contact[] c =(Contact[]) paraMap.get(string);
task.addScriptableJavaObject("reslut", c);
}if("urlPath".equals(string)){
task.addScriptableJavaObject(string, paraMap.get("urlPath"));
}else{
//List<Contact> list = (ArrayList<Contact>)paraMap.get(string);
List list =(ArrayList) paraMap.get(string);
task.addScriptableJavaObject(string, list);
}
}
}

//Set rendering options - such as file or stream output,
//output format, whether it is embeddable, etc
HTMLRenderOption options = new HTMLRenderOption();
Date dateNow=new Date();
String reqTime = CommonFunc.ConvertDateToStr(new Date(), Constants.YYYYMM);
String dateNowStr= reqTime +"/"+outFlie;
String birtFile = realPath+ Constants.birtPath+dateNowStr;
options.setOutputFileName(birtFile);
options.setOutputFormat(outType);
task.setRenderOption(options);

//run the report and destroy the engine
try{
task.run();
engine.destroy();
}catch (Exception e) {
e.printStackTrace();
}

return dateNowStr;
}
public static void main(String[] args) throws Exception {
Map<String, Object> reslutMap = new HashMap<String, Object>();
ExecuteReport pf = new ExecuteReport();
pf.executeReport(reslutMap,"scripted.rptdesign","scripted.xls","xls");
}
}


Reporttest类

package com.jshx;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jshx.util.CommonFunc;
import com.jshx.util.Constants;

public class Reporttest {
public static void main(String args[]) {
try {
ContactListFactory cf = new ContactListFactory();
Contact[] reslut = cf.createContactList();
List<Contact> reslutList = cf.createList();
Map<String, Object> reslutMap = new HashMap<String, Object>();
reslutMap.put("reslut", reslut);
reslutMap.put("reslutList", reslutList);
String reqTime = CommonFunc.ConvertDateToStr(new Date(),Constants.YYYYMMDDHHMMSS);
String birtType = "html";
String birtFileName = "kenScripDataset_" + reqTime + "." + birtType;
ExecuteReport mreport = new ExecuteReport();
mreport.executeReport(reslutMap, "kenScripDataset.rptdesign",birtFileName, birtType);
} catch (Exception e) {
e.printStackTrace();
}
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值