BIRT Design API 学习

0 篇文章 0 订阅
DECreateDynamicTable.java  例子code : 

package com.shentong.krtDesigner;

import java.io.IOException;
import java.util.ArrayList;

import org.eclipse.birt.core.framework.Platform;
import org.eclipse.birt.report.model.api.CellHandle;
import org.eclipse.birt.report.model.api.DataItemHandle;
import org.eclipse.birt.report.model.api.DesignConfig;
import org.eclipse.birt.report.model.api.ElementFactory;
import org.eclipse.birt.report.model.api.GridHandle;
import org.eclipse.birt.report.model.api.IDesignEngine;
import org.eclipse.birt.report.model.api.IDesignEngineFactory;
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.ParamBindingHandle;
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.ScalarParameterHandle;
import org.eclipse.birt.report.model.api.SessionHandle;
import org.eclipse.birt.report.model.api.StructureFactory;
import org.eclipse.birt.report.model.api.TableHandle;
import org.eclipse.birt.report.model.api.TextItemHandle;
import org.eclipse.birt.report.model.api.activity.SemanticException;
import org.eclipse.birt.report.model.api.elements.DesignChoiceConstants;
import org.eclipse.birt.report.model.api.elements.structures.ComputedColumn;

import com.ibm.icu.util.ULocale;

/**
 * Dynamic Table BIRT Design Engine API (DEAPI) demo.
 */

public class DECreateDynamicTable {
	ReportDesignHandle designHandle = null;
	ElementFactory designFactory = null;
	StructureFactory structFactory = null;

	public static void main(String[] args) {
		try {
			DECreateDynamicTable de = new DECreateDynamicTable();
			ArrayList params = new ArrayList();
			params.add("EMPLOYEENUMBER");
			params.add("LASTNAME");
			params.add("FIRSTNAME");
			params.add("EXTENSION");
			params.add("EMAIL");
			params.add("OFFICECODE");
			params.add("REPORTSTO");
			params.add("JOBTITLE");

			de.buildReport(params, "From CLASSICMODELS.EMPLOYEES");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SemanticException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	void buildDataSource() throws SemanticException {

		OdaDataSourceHandle dsHandle = designFactory.newOdaDataSource("数据源",
				"org.eclipse.birt.report.data.oda.jdbc");
		dsHandle.setProperty("odaDriverClass",
				"com.shentong.kfront.report.data.oda.sampledb.Driver");
		dsHandle.setProperty("odaURL", "jdbc:classicmodels:sampledb");
		dsHandle.setProperty("odaUser", "ClassicModels");
		dsHandle.setProperty("odaPassword", "");

		designHandle.getDataSources().add(dsHandle);

	}

	void buildDataSet(ArrayList cols, String fromClause)
			throws SemanticException {

		OdaDataSetHandle dsHandle = designFactory.newOdaDataSet("数据集",
				"org.eclipse.birt.report.data.oda.jdbc.JdbcSelectDataSet");
		dsHandle.setDataSource("数据源");
		String qry = "Select ";
		for (int i = 0; i < cols.size(); i++) {
			qry += " " + cols.get(i);
			if (i != (cols.size() - 1)) {
				qry += ",";
			}

		}
		qry += " " + fromClause;

		dsHandle.setQueryText(qry);

		designHandle.getDataSets().add(dsHandle);

	}

	void buildReport(ArrayList cols, String fromClause) throws IOException,
			SemanticException {

		// Configure the Engine and start the Platform
		DesignConfig config = new DesignConfig();

		config.setProperty("BIRT_HOME", "D:\\Jar包\\birt-lib");
		IDesignEngine engine = null;
		try {

			Platform.startup(config);
			IDesignEngineFactory factory = (IDesignEngineFactory) Platform
					.createFactoryObject(IDesignEngineFactory.EXTENSION_DESIGN_ENGINE_FACTORY);
			engine = factory.createDesignEngine(config);

		} catch (Exception ex) {
			ex.printStackTrace();
		}

		SessionHandle session = engine.newSessionHandle(ULocale.ENGLISH);

		try {
			// open a design or a template
			designHandle = session.createDesign();
			designFactory = designHandle.getElementFactory();
			buildDataSource();
			buildDataSet(cols, fromClause);

			GridHandle grid = designFactory.newGridItem("grid", 1, 3);
			grid.setWidth("100%");
			designHandle.getBody().add(grid);

			RowHandle row = (RowHandle) grid.getRows().get(0);
			CellHandle cell1 = (CellHandle) row.getCells().get(0);

			LabelHandle label = designFactory.newLabel("title");
			label.setText("通过API创建报表");
			label.setProperty("fontSize", "14pt");
			label.setProperty("fontWeight", "bold");
			label.setProperty("textAlign", "center");
			cell1.getContent().add(label);
			
			
			ScalarParameterHandle parameterHandle = designFactory.newScalarParameter("新参数");
			parameterHandle.setControlType(DesignChoiceConstants.PARAM_CONTROL_LIST_BOX);
			parameterHandle.setValueType(DesignChoiceConstants.PARAM_VALUE_TYPE_DYNAMIC);
			parameterHandle.setDataSetName("数据集");
			parameterHandle.setValueExpr("dataSetRow[\"EMPLOYEENUMBER\"]");
			designHandle.getParameters().add(parameterHandle);

			TableHandle table = designFactory
					.newTableItem("table", cols.size());
			table.setWidth("100%");
			table.setDataSet(designHandle.findDataSet("数据集"));

			// 这段代码是有用处的,  数据绑定
			PropertyHandle computedSet = table.getColumnBindings();
			ComputedColumn cs1 = null;

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

			// table header
			RowHandle tableheader = (RowHandle) table.getHeader().get(0);
			for (int i = 0; i < cols.size(); i++) {
				LabelHandle label1 = designFactory.newLabel((String) cols
						.get(i));
				label1.setText((String) cols.get(i));
				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.size(); i++) {
				CellHandle cell = (CellHandle) tabledetail.getCells().get(i);
				DataItemHandle data = designFactory.newDataItem("data_"
						+ (String) cols.get(i));
				data.setResultSetColumn((String) cols.get(i));
				cell.getContent().add(data);
			}

			// grid.getRows().get(0).add(table);
			RowHandle row2 = (RowHandle) grid.getRows().get(1);
			CellHandle cell2 = (CellHandle) row2.getCells().get(0);
			cell2.getContent().add(table);

			TextItemHandle text = designFactory.newTextItem(null);
			text.setContent("您选择的参数是<VALUE-OF>params[\"新参数\"].value</VALUE-OF>");
			text.setContentType(DesignChoiceConstants.TEXT_CONTENT_TYPE_HTML);
			text.setProperty("textAlign", "center");
			// Save the design and close it.

			RowHandle row3 = (RowHandle) grid.getRows().get(2);
			CellHandle cell3 = (CellHandle) row3.getCells().get(0);
			cell3.getContent().add(text);

			designHandle.saveAs("D:/DeCreateDynamic.krt"); //$NON-NLS-1$
			designHandle.close();
			System.out.println("DE Finished");
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
}


这个例子演示了从建立DataSource ,然后建立DataSet , 动态的根据输入数据输出report template .
关于Birt 的 API , 在 eclipse 的 help content 里面有,3.3 支持新的基于topic 的search ,可以帮我们简化搜索的topic , 其中有五个API (一共是5个) : Report Object Model API , Report Engine API , Birt Report Scripting API , Open Data Access API , Data Engine API .另外也提供详细的讲解每一个report 的元素的意思.非常好的一份资料 .


 
 



这个例子一共有四个函数 :
1 . Main 函数:  这个例子简单之处在与它可以直接的运行,只要你修改了
     config.setProperty("BIRT_HOME", "C:/birt-runtime-2_1_1/birt-runtime-2_1_1/ReportEngine");    指向你自己的Birt Runtime 解压后的ReportEngine 目录.
     designHandle = session.openDesign("c:/tmp/testdeapi.rptdesign");                             你可以从Birt 里面建立一个新的Report template.然后指向这个report 就可以了
     designHandle.saveAs( "c:/temp/sample.rptdesign" ); //$NON-NLS-1$                             指定一个你想保存的位置,c:/temp  目录存在你才能够保存到c:/temp 目录下.
2 . buildDataSource   函数把一个ReportDesignHandle 的 Data Source 初始化, setProperties 左边的String 是不能变的,Data Source 的名字可以随便取,取DataSet 的时候要根据这个名字来取.
3 . buildDataSet      通过拼sql 的方式 ,来build DataSet, 注意sql 别拼错了.
4 . buildReport       注意element 的初始化顺序.在所有的DataItem 外面都是一层Cell,Cell 外面才是row .这个例子使用的row 来拼成table 的,也可以用column 来拼,相对应的数据处理也是一个column 一个 column 的处理的了.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值