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();
}
}
利用BIRT API生成报表例子1
原创
©著作权归作者所有:来自51CTO博客作者pyzheng的原创作品,请联系作者获取转载授权,否则将追究法律责任

提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
利用BIRT API生成报表例子3
代码来自网络[code="java"]package com.test;import java.io.InputStream;impo
开发工具 java eclipse i++ -
使用BIRT API创建交叉报表1开发工具 java eclipse html
-
PB利用SaveAs生成PDF报表
利用pb中的SaveAs函数生成PDF
Adobe pb SaveAs -
python模糊PID python模糊匹配库
Python进行模糊匹配
python 描述文件 App sed