利用BIRT API生成报表例子1

[url]http://www.birthome.com/read.php?tid-2762.html[/url]

package cn.net.fone.webbirt; 

import java.io.IOException;
import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.eclipse.birt.report.engine.api.EngineConstants;
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.IReportEngine;
import org.eclipse.birt.report.model.api.CellHandle;
import org.eclipse.birt.report.model.api.DataItemHandle;
import org.eclipse.birt.report.model.api.ElementFactory;
import org.eclipse.birt.report.model.api.LabelHandle;
import org.eclipse.birt.report.model.api.OdaDataSetHandle;
import org.eclipse.birt.report.model.api.OdaDataSourceHandle;
import org.eclipse.birt.report.model.api.PropertyHandle;
import org.eclipse.birt.report.model.api.ReportDesignHandle;
import org.eclipse.birt.report.model.api.RowHandle;
import org.eclipse.birt.report.model.api.StructureFactory;
import org.eclipse.birt.report.model.api.TableHandle;
import org.eclipse.birt.report.model.api.activity.SemanticException;
import org.eclipse.birt.report.model.api.elements.structures.ComputedColumn;
import org.eclipse.birt.report.model.api.olap.CubeHandle;
import org.eclipse.birt.report.model.api.olap.DimensionHandle;
import org.eclipse.birt.report.engine.api.HTMLServerImageHandler;

public class DesignReportServlet extends HttpServlet{
/**
*
*/
private static final long serialVersionUID = 1L;
/**
* Constructor of the object.
*/
private IReportEngine birtReportEngine = null;
protected static Logger logger = Logger.getLogger( "org.eclipse.birt" );

public DesignReportServlet() {
super();
}

/**
* Destruction of the servlet.

*/
public void destroy() {
super.destroy();
BirtEngine.destroyBirtEngine();
}


/**
* The doGet method of the servlet.
select t.count_date,t.cha_user_name,t.type_name,t.city_name from t_yx_pv_count t where t.count_date=to_date('2012-06-26','yyyy-MM-dd');
*
*/
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {

//get report name and launch the engine
resp.setContentType("text/html");
//resp.setContentType( "application/pdf" );
//resp.setHeader ("Content-Disposition","inline; filename=test.pdf");
//String reportName = req.getParameter("__report");
String[] cols = (String[])req.getParameterMap().get("dyna1");
ServletContext sc = req.getSession().getServletContext();
this.birtReportEngine = BirtEngine.getBirtEngine(sc);

IReportRunnable design;
try
{
//Open report design
design = birtReportEngine.openReportDesign(sc.getRealPath("/Reports")+"/"+"designReport.rpttemplate");
ReportDesignHandle report = (ReportDesignHandle) design.getDesignHandle( );
buildReport( cols, report );

//create task to run and render report
IRunAndRenderTask task = birtReportEngine.createRunAndRenderTask( design );

// HashMap paramMap1 = new HashMap();
// DateFormat dFormat=new SimpleDateFormat("yyyy-MM-dd");
// Long RP_st = dFormat.parse("2012-06-24").getTime();
// Long RP_et = dFormat.parse("2012-06-25").getTime();
// paramMap1.put("RP_st",new java.sql.Date(RP_st));
// paramMap1.put("RP_et",new java.sql.Date(RP_et));
// paramMap1.put("cid","402880c73726bdb9013759864237000b");
// paramMap1.put("tid", 0);
// paramMap1.put("cityids","");

//task.setAppContext( paramMap1 );

//set output options
HTMLRenderOption options = new HTMLRenderOption();
options.setImageHandler( new HTMLServerImageHandler() );
options.setImageDirectory( sc.getRealPath("/images"));
options.setBaseImageURL( req.getContextPath() + "/images" );
options.setOutputFormat(HTMLRenderOption.OUTPUT_FORMAT_HTML);
//options.setOutputFormat(HTMLRenderOption.OUTPUT_FORMAT_PDF);
options.setOutputStream(resp.getOutputStream());
task.setRenderOption(options);

//run report
task.run();
task.close();
}catch (Exception e){

e.printStackTrace();
throw new ServletException( e );
}
}

public void buildReport(String[] cols, ReportDesignHandle designHandle){


try{
ElementFactory designFactory = designHandle.getElementFactory( );

buildDataSource(designFactory, designHandle);


//ArrayList cols = new ArrayList();
//cols.add("OFFICECODE");
//cols.add("CITY");
//cols.add("COUNTRY");

buildDataSet(cols, "FROM T_YX_PV_COUNT", designFactory, designHandle);

TableHandle table = designFactory.newTableItem( "table", cols.length );
table.setWidth( "100%" );
table.setDataSet( designHandle.findDataSet( "ds" ) );

CubeHandle cubeHandle=designFactory.newOdaCube("cube");
DimensionHandle dimensionHandle = cubeHandle.getDimension("dddd");
//cubeHandle.
//dimensionHandle.

PropertyHandle computedSet = table.getColumnBindings( );
ComputedColumn cs1 = null;

for( int i=0; i < cols.length; i++){
cs1 = StructureFactory.createComputedColumn();
cs1.setName((String)cols);
cs1.setExpression("dataSetRow[\"" + (String)cols + "\"]");
computedSet.addItem(cs1);
}


// table header
RowHandle tableheader = (RowHandle) table.getHeader( ).get( 0 );


for( int i=0; i < cols.length; i++){
LabelHandle label1 = designFactory.newLabel( (String)cols );
label1.setText((String)cols);
CellHandle cell = (CellHandle) tableheader.getCells( ).get( i );
cell.getContent( ).add( label1 );
}

// table detail
RowHandle tabledetail = (RowHandle) table.getDetail( ).get( 0 );
for( int i=0; i < cols.length; i++){
CellHandle cell = (CellHandle) tabledetail.getCells( ).get( i );
DataItemHandle data = designFactory.newDataItem( "data_"+(String)cols );
data.setResultSetColumn( (String)cols);
cell.getContent( ).add( data );
}



designHandle.getBody( ).add( table );
}catch(Exception e){
e.printStackTrace();
}

}

void buildDataSource( ElementFactory designFactory, ReportDesignHandle designHandle ) throws SemanticException
{

OdaDataSourceHandle dsHandle = designFactory.newOdaDataSource(
"Data Source", "org.eclipse.birt.report.data.oda.jdbc" );
dsHandle.setProperty( "odaDriverClass","oracle.jdbc.driver.OracleDriver" );
dsHandle.setProperty( "odaURL", "" );
dsHandle.setProperty( "odaUser", "" );
dsHandle.setProperty( "odaPassword", "" );

designHandle.getDataSources( ).add( dsHandle );

}
/*
* select t.count_date,t.cha_user_name,t.type_name,t.city_name from t_yx_pv_count t where t.count_date=to_date('2012-06-26','yyyy-MM-dd');
* */
void buildDataSet(String[] cols, String fromClause, ElementFactory designFactory, ReportDesignHandle designHandle ) throws SemanticException
{

OdaDataSetHandle dsHandle = designFactory.newOdaDataSet( "ds",
"org.eclipse.birt.report.data.oda.jdbc.JdbcSelectDataSet" );
dsHandle.setDataSource( "Data Source" );
String qry = "select ";
for( int i=0; i < cols.length; i++){
qry += " " + cols;
if( i != (cols.length -1) ){
qry += ",";
}

}
qry += " " + fromClause + " where COUNT_DATE=to_date('2012-06-26','yyyy-MM-dd')";

dsHandle.setQueryText( qry );

designHandle.getDataSets( ).add( dsHandle );
}
/**
* The doPost method of the servlet.

*
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.println(" Post Not Supported");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}

/**
* Initialization of the servlet.

*
* @throws ServletException if an error occure
*/
public void init() throws ServletException {
BirtEngine.initBirtConfig();

}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值