利用BIRT API生成报表例子2

[url]http://www.birthome.com/read.php?tid=471[/url]

package com.demo;

public class Bean
{
private String name;
private int age;
private String city;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}

}



package com.demo;


import java.util.ArrayList;
import java.util.List;

public class Factory
{
@SuppressWarnings({ "rawtypes", "unchecked" })
public static List getData()
{
Bean b1=new Bean();
b1.setName("张三");
b1.setAge(18);
b1.setCity("西安");

Bean b2=new Bean();
b2.setName("李四");
b2.setAge(25);
b2.setCity("北京");

Bean b3=new Bean();
b3.setName("王五");
b3.setAge(40);
b3.setCity("深圳");

Bean b4=new Bean();
b4.setName("陈六");
b4.setAge(30);
b4.setCity("香港");

Bean b5=new Bean();
b5.setName("Jack");
b5.setAge(30);
b5.setCity("New York");

Bean b6=new Bean();
b6.setName("Tom");
b6.setAge(30);
b6.setCity("London");

Bean b7=new Bean();
b7.setName("Jim");
b7.setAge(30);
b7.setCity("LA");

List list=new ArrayList();
list.add(b1);
list.add(b2);
list.add(b3);
list.add(b4);
list.add(b5);
list.add(b6);
list.add(b7);
return list;
}

}



package com.demo;

import java.util.ArrayList;
import java.util.List;

public class TestData
{
@SuppressWarnings({ "rawtypes", "unchecked" })
public static List getColumn()
{
List list=new ArrayList();

String[] strArray=new String [3];
strArray[0]="CPU,显示器";
strArray[1]="利用率1,利用率2,磁盘";
strArray[2]="空闲率";

String[] strArray2=new String [2];
strArray2[0]="磁盘";
strArray2[1]="吞吐量";

String[] strArray3=new String [4];
strArray3[0]="内存";
strArray3[1]="内存量,二级缓存";
strArray3[2]="最大利用率";
strArray3[3]="测试1,测试2,测试3";

list.add(strArray);
list.add(strArray2);
list.add(strArray3);
return list;
}

}


[b][color=red]主要程序代码[/color][/b]
package com.demo;

import java.io.IOException;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
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.DesignElementHandle;
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.ImageHandle;
import org.eclipse.birt.report.model.api.LabelHandle;
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.ScriptDataSetHandle;
import org.eclipse.birt.report.model.api.ScriptDataSourceHandle;
import org.eclipse.birt.report.model.api.SessionHandle;
import org.eclipse.birt.report.model.api.SlotHandle;
import org.eclipse.birt.report.model.api.StructureFactory;
import org.eclipse.birt.report.model.api.StyleHandle;
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.CachedMetaData;
import org.eclipse.birt.report.model.api.elements.structures.ColumnHint;
import org.eclipse.birt.report.model.api.elements.structures.ComputedColumn;
import org.eclipse.birt.report.model.api.elements.structures.ResultSetColumn;

import com.ibm.icu.util.ULocale;

/**
*
* @author ZYJ
*
*/
public class Demo4 {
public static void main(String[] args) {
try {

buildReport();
} catch (IOException e) {
e.printStackTrace();
} catch (SemanticException e) {
e.printStackTrace();
}
}

static void buildReport() throws IOException, SemanticException {
DesignConfig config = new DesignConfig();
config.setProperty("BIRT_HOME", "F:/movie/birt-runtime-2_5_1/birt-runtime-2_5_1/ReportEngine");
IDesignEngine engine = null;
try {
Platform.startup(config);
IDesignEngineFactory factory = (IDesignEngineFactory) Platform
.createFactoryObject(IDesignEngineFactory.EXTENSION_DESIGN_ENGINE_FACTORY);
engine = factory.createDesignEngine(config);
} catch (Exception e) {
e.printStackTrace();
}
SessionHandle session = engine.newSessionHandle(ULocale.CHINESE);
ReportDesignHandle design = session.createDesign();
ElementFactory factory = design.getElementFactory();
DesignElementHandle element = factory.newSimpleMasterPage("Demo3");
design.getMasterPages().add(element);

// ///
ScriptDataSourceHandle datasource = buidDataSource(factory);
ScriptDataSetHandle dataset = createScript(datasource, factory);
design.getDataSources().add(datasource);
design.getDataSets().add(dataset);
SetStyle(design, factory);
// /
createTitle("磁盘IO吞吐量报表", "admin", "2009-10-01 00:00:00", "2009-11-15 00:00:00", design, factory);
TableHandle table = factory.newTableItem(null, 3);
table.setWidth("100%");
table.setDataSet(dataset);
design.getBody().add(table);
RowHandle tableheader = (RowHandle) table.getHeader().get(0);
createColumn(TestData.getColumn(), tableheader, factory);
RowHandle tablebody = (RowHandle) table.getDetail().get(0);
initBody(TestData.getColumn(), tablebody, factory);

// //
// /将数据集绑定要表格中

PropertyHandle computedSet = table.getColumnBindings();
ComputedColumn computedColum = null;
computedColum = StructureFactory.createComputedColumn();
computedColum.setName("name");
computedColum.setExpression("dataSetRow['name']");
computedSet.addItem(computedColum);
ComputedColumn computedColum2 = null;
computedColum2 = StructureFactory.createComputedColumn();
computedColum2.setName("age");
computedColum2.setExpression("dataSetRow['age']");
computedSet.addItem(computedColum2);
ComputedColumn computedColum3 = null;
computedColum3 = StructureFactory.createComputedColumn();
computedColum3.setName("city");
computedColum3.setExpression("dataSetRow['city']");
computedSet.addItem(computedColum3);

CellHandle celltotal = (CellHandle) tablebody.getCells().get(0);
SlotHandle grid = celltotal.getContent();
GridHandle grid2 = (GridHandle) grid.get(0);
RowHandle row = (RowHandle) grid2.getRows().get(0);
CellHandle cell = (CellHandle) row.getCells().get(0);
DataItemHandle data = factory.newDataItem("data_name");
data.setResultSetColumn("name");
cell.getContent().add(data);

CellHandle cella = (CellHandle) tablebody.getCells().get(1);
SlotHandle grida = cella.getContent();
GridHandle grida2 = (GridHandle) grida.get(1);
RowHandle rowa = (RowHandle) grida2.getRows().get(0);
CellHandle cellaa = (CellHandle) rowa.getCells().get(0);
DataItemHandle dataa = factory.newDataItem("data_age");
dataa.setResultSetColumn("age");
cellaa.getContent().add(dataa);

ImageHandle image = factory.newImage(null);
CellHandle celltotalimg = (CellHandle) tablebody.getCells().get(2);
GridHandle gridimg = (GridHandle) celltotalimg.getContent().get(0);
RowHandle rowimg = (RowHandle) gridimg.getRows().get(0);
CellHandle cellimg = (CellHandle) rowimg.getCells().get(0);

image.setFile("\"D:/lineXY.png\"");
image.setProperty("height", "200px");
image.setProperty("width", "300px");
cellimg.getContent().add(image);

CellHandle cellb = (CellHandle) tablebody.getCells().get(0);
SlotHandle gridb = cellb.getContent();
GridHandle gridb2 = (GridHandle) gridb.get(2);
RowHandle rowb = (RowHandle) gridb2.getRows().get(0);
CellHandle cellbb = (CellHandle) rowb.getCells().get(0);
DataItemHandle datab = factory.newDataItem("data_city");
datab.setResultSetColumn("city");
cellbb.getContent().add(datab);
// /

// /
design.saveAs("d:/demo.rptdesign");
design.close();
System.out.println("Finished");
}

/**
*
* @param text
* 报表模板标题
* @param userId
* 产生用户
* @param start
* 开始时间
* @param end
* 结束时间
* @param design
* 报表实例
* @param factory
* birt报表元素工厂
* @throws SemanticException
*/
public static void createTitle(String text, String userId, String start, String end, ReportDesignHandle design,
ElementFactory factory) throws SemanticException {
LabelHandle title = factory.newLabel(null);
title.setText(text);
design.getBody().add(title);
title.setProperty(StyleHandle.FONT_SIZE_PROP, "20");
title.setProperty(StyleHandle.TEXT_ALIGN_PROP, "center");
title.setProperty(StyleHandle.FONT_WEIGHT_PROP, "Bold");
LabelHandle subhead = factory.newLabel(null);
subhead.setText("报表用户:" + userId + " 时间范围:" + start + "至" + end);
design.getBody().add(subhead);
subhead.setProperty(StyleHandle.FONT_SIZE_PROP, "12");
subhead.setProperty(StyleHandle.TEXT_ALIGN_PROP, "center");
subhead.setProperty(StyleHandle.FONT_WEIGHT_PROP, "Bold");
}

// 初始化表头
@SuppressWarnings("rawtypes")
public static void createColumn(List columnlist, RowHandle row, ElementFactory factory) throws SemanticException {
logger.info("rpt info:zyj 开始初始化表头结构");
for (int i = 0; i < columnlist.size(); i++) {
String[] columnarry = (String[]) columnlist.get(i);
CellHandle cell = (CellHandle) row.getCells().get(i);
GridHandle gridcell = factory.newGridItem(null, 1, 1);
cell.getContent().add(gridcell);
cell.setStyleName("BorderStyle");
for (int j = 0; j < columnarry.length; j++) {
String[] columnrow = columnarry[j].split(",");
GridHandle grid = factory.newGridItem(null, columnrow.length, 1); // 插入一个grid
// cell.getContent().add(grid);
RowHandle row2 = (RowHandle) gridcell.getRows().get(0);
((CellHandle) row2.getCells().get(0)).getContent().add(grid);
RowHandle temprow = (RowHandle) grid.getRows().get(0);
for (int k = 0; k < columnrow.length; k++) {
LabelHandle label = factory.newLabel(null);
CellHandle tempcell = (CellHandle) temprow.getCells().get(k);
tempcell.setStyleName("BorderStyle");
tempcell.getContent().add(label);
label.setText(columnrow[k]);
}
}
}
logger.info("rpt info:zyj 表头结构初始化完成");
}

// //初始化表体
@SuppressWarnings({ "rawtypes", "unused" })
public static void initBody(List columnlist, RowHandle row, ElementFactory factory) throws SemanticException {
logger.info("rpt info:zyj 开始初始化表体结构结构");
for (int i = 0; i < columnlist.size(); i++) {
String[] columnarry = (String[]) columnlist.get(i);
CellHandle cell = (CellHandle) row.getCells().get(i);
cell.setStyleName("BorderStyle");
for (int j = 0; j < columnarry.length; j++) {
String[] columnrow = columnarry[j].split(",");
GridHandle grid = factory.newGridItem(null, columnrow.length, 1); // 插入一个grid
cell.getContent().add(grid);
RowHandle temprow = (RowHandle) grid.getRows().get(0);

for (int k = 0; k < columnrow.length; k++) {
LabelHandle label = factory.newLabel(null);
CellHandle tempcell = (CellHandle) temprow.getCells().get(k);
tempcell.setStyleName("BorderStyle");
}

}
}
logger.info("rpt info:zyj 表体结构初始化完成");
}

// /创建数据源
public static ScriptDataSourceHandle buidDataSource(ElementFactory factory) throws SemanticException {
ScriptDataSourceHandle datasource = factory.newScriptDataSource("Datasource");
return datasource;

}

// //创建数据集
public static ScriptDataSetHandle createScript(ScriptDataSourceHandle datasource, ElementFactory factory)
throws SemanticException {
ScriptDataSetHandle dataset = factory.newScriptDataSet("Dataset");
dataset.setDataSource("Datasource");
addFlowResult(dataset);
dataset.setOpen("importPackage(Packages.birt); emps = Factory.getData(); iterator = emps.iterator();");
dataset.setFetch("if(iterator.hasNext()==false) {return false;} var emp=iterator.next(); row[1]=emp.getName() ;row[2]=emp.getAge(); row[3]=emp.getCity(); return true;");
dataset.setClose("emps=null;iterator = null;emp = null;");
return dataset;
}

// ///设定数据集输出列
public static ScriptDataSetHandle addFlowResult(ScriptDataSetHandle dataset) throws SemanticException {
PropertyHandle resultSet = dataset.getPropertyHandle(ScriptDataSetHandle.RESULT_SET_HINTS_PROP);

ResultSetColumn rs1 = StructureFactory.createResultSetColumn();
rs1.setPosition(1);
rs1.setColumnName("name");
rs1.setDataType("String");
resultSet.addItem(rs1);
ResultSetColumn rs2 = StructureFactory.createResultSetColumn();
rs2.setPosition(2);
rs2.setColumnName("age");
rs2.setDataType("Integer");
resultSet.addItem(rs2);
ResultSetColumn rs3 = StructureFactory.createResultSetColumn();
rs3.setPosition(3);
rs3.setColumnName("city");
rs3.setDataType("String");
resultSet.addItem(rs3);
resultSet = dataset.getPropertyHandle(ScriptDataSetHandle.COLUMN_HINTS_PROP);
ColumnHint columnHint1 = StructureFactory.createColumnHint();
columnHint1.setProperty("columnName", "name");
resultSet.addItem(columnHint1);
ColumnHint columnHint2 = StructureFactory.createColumnHint();
columnHint2.setProperty("columnName", "age");
resultSet.addItem(columnHint2);
ColumnHint columnHint3 = StructureFactory.createColumnHint();
columnHint3.setProperty("columnName", "city");
resultSet.addItem(columnHint3);
CachedMetaData cache = StructureFactory.createCachedMetaData();
dataset.setCachedMetaData(cache);
return dataset;
}

// //设定style
public static void SetStyle(ReportDesignHandle design, ElementFactory factory) throws SemanticException {
StyleHandle BorderStyle = factory.newStyle("BorderStyle");
BorderStyle.setProperty(StyleHandle.BORDER_TOP_STYLE_PROP, "Groove");
BorderStyle.setProperty(StyleHandle.BORDER_BOTTOM_STYLE_PROP, "Groove");
BorderStyle.setProperty(StyleHandle.BORDER_LEFT_STYLE_PROP, "Groove");
BorderStyle.setProperty(StyleHandle.BORDER_RIGHT_STYLE_PROP, "Groove");
BorderStyle.setProperty(StyleHandle.BORDER_BOTTOM_WIDTH_PROP, "1px");
BorderStyle.setProperty(StyleHandle.BORDER_LEFT_WIDTH_PROP, "1px");
BorderStyle.setProperty(StyleHandle.BORDER_RIGHT_WIDTH_PROP, "1px");
BorderStyle.setProperty(StyleHandle.BORDER_TOP_WIDTH_PROP, "1px");
design.getStyles().add(BorderStyle);
}

private static final Log logger = LogFactory.getLog(Demo4.class);

}
// 最近这几天一直在研究birt报表,这个是这几天研究的结果,发上来大家一起分享下,如何使用birt的api完整的创建一个报表,由于我这个只是个例子,很多东西都是写死的,
// //不过还是希望能对正在学习birt api使用的各位朋友有所帮助,最近又在研究birt api的参数传递,话说这资料更少了,头痛
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值