BIRT的Java实现

3 篇文章 3 订阅
2 篇文章 0 订阅

这东西捯饬了好长时间都不成功,还好又仔细找了官网入门资料,才做出一个简单的东西。哭


事先需要安装Tomcat,具体步骤参考birt官网教程:

www.eclipse.org/birt/documentation/integrating/viewer-setup.php

官方提供的代码:

import java.io.IOException;

import org.eclipse.birt.core.framework.Platform;
import org.eclipse.birt.report.model.api.CellHandle;
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.ReportDesignHandle;
import org.eclipse.birt.report.model.api.RowHandle;
import org.eclipse.birt.report.model.api.SessionHandle;
import org.eclipse.birt.report.model.api.activity.SemanticException; 

import com.ibm.icu.util.ULocale;

/**
 * Simple BIRT Design Engine API (DEAPI) demo.
 */
public class SimpleCreate
{
   public static void main( String[] args )
   {
       try
       {
           buildReport( );
       }
       catch( IOException e )
       {
           e.printStackTrace();
       }
       catch( SemanticException e )
       {
           e.printStackTrace();
       }
   }
	
   // This method shows how to build a very simple BIRT report with a
   // minimal set of content: a simple grid with an image and a label.
 	
   static void buildReport() throws IOException, SemanticException
   {
       // Create a session handle. This is used to manage all open designs.
       // Your app need create the session only once.
 		
       //Configure the Engine and start the Platform 
       DesignConfig config = new DesignConfig( );//org.eclipse.birt.report.model.api.DesignConfig

       config.setProperty("BIRT_HOME", "D:/birt-runtime-4_5_0/ReportEngine");
       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 ) ;
		
       // Create a new report design.
       ReportDesignHandle design = session.createDesign( );
		
       // The element factory creates instances of the various BIRT elements.
       ElementFactory efactory = design.getElementFactory( );
		
       // Create a simple master page that describes how the report will appear when printed.
       //
       // Note: The report will fail to load in the BIRT designer unless you create a master page.
       DesignElementHandle element = efactory.newSimpleMasterPage( "Page Master" ); 
       design.getMasterPages( ).add( element );
		
       // Create a grid and add it to the "body" slot of the report design.
       GridHandle grid = efactory.newGridItem( null, 2 /* cols */, 1 /* row */ );
	design.getBody( ).add( grid );
		
       // Note: Set the table width to 100% to prevent the label
       // from appearing too narrow in the layout view.
       grid.setWidth( "100%" ); 
		
       // Get the first row.
       RowHandle row = (RowHandle) grid.getRows( ).get( 0 );
		
       // Create an image and add it to the first cell.
       ImageHandle image = efactory.newImage( null );
       CellHandle cell = (CellHandle) row.getCells( ).get( 0 );
       cell.getContent( ).add( image );
       image.setURL( "\"urlofimage\"" ); 
		
       // Create a label and add it to the second cell.
       LabelHandle label = efactory.newLabel( null );
       cell = (CellHandle) row.getCells( ).get( 1 );
       cell.getContent( ).add( label );
       label.setText( "Hello, world!" ); 
		
       // Save the design and close it.
       design.saveAs( "d:/Program Files/apache-tomcat-8.5.2/webapps/birt/work.rptdesign" ); //将报表文件放在Tomcat目录下以便显示
       design.close( );
       System.out.println("Finished");
       // We're done!
   }
}

运行代码之前,需要把birt-runtime中/ReportEngine/lib文件夹下的所有jar包导入。

程序成功运行后会在Tomcat的birt目录下新建work.rptdesign报表文件,浏览器登陆http://localhost:8080/birt/frameset?__report=work.rptdesign即可显示结果。

源码具体含义可参考官网API

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值