birt使用quartz框架实现自动生成报表

    经过两天的学习,终于通过quartz框架将birt的自动生成报表功能实现了,下面将代码粘贴上来,仅供参考。

   1、GenReportAutoDs类,用来生成报表。其中资源库文件位于工程根目录下的library下,因此需要单独设置,另外报表需要两个参数,也一并赋值。

package cn.xxt.test;

import java.util.Date;

import javax.servlet.ServletContext;

import org.eclipse.birt.core.framework.IPlatformContext;
import org.eclipse.birt.core.framework.Platform;
import org.eclipse.birt.core.framework.PlatformServletContext;
import org.eclipse.birt.report.engine.api.EngineConfig;
import org.eclipse.birt.report.engine.api.EngineConstants;
import org.eclipse.birt.report.engine.api.HTMLRenderOption;
import org.eclipse.birt.report.engine.api.IReportEngine;
import org.eclipse.birt.report.engine.api.IReportEngineFactory;
import org.eclipse.birt.report.engine.api.IReportRunnable;
import org.eclipse.birt.report.engine.api.IRunAndRenderTask;
import org.springframework.web.context.ContextLoader;

import cn.xxt.util.DateUtil;

public class GenReportAutoDs {
 @SuppressWarnings("unchecked")
 public void runReport() {
  System.out.println(DateUtil.format(new Date(), "HH:mm:ss", false)+":开始生成包含library报表……");
  IReportEngine birtEngine = null;
  EngineConfig config = new EngineConfig();
  ServletContext servletContext = ContextLoader.getCurrentWebApplicationContext().getServletContext();
  IPlatformContext context = new PlatformServletContext(servletContext);
  String path = servletContext.getRealPath("/report");
  config.setPlatformContext(context);
  config.setResourcePath(servletContext.getRealPath("/library"));
  try {
   Platform.startup(config);
   IReportEngineFactory factory = (IReportEngineFactory) Platform
     .createFactoryObject(IReportEngineFactory.EXTENSION_REPORT_ENGINE_FACTORY);
   birtEngine = factory.createReportEngine(config);
   IReportRunnable design = null;
   design = birtEngine.openReportDesign(path
     + "/test/cascading.rptdesign");
   IRunAndRenderTask task = birtEngine.createRunAndRenderTask(design);
   task.getAppContext().put(
     EngineConstants.APPCONTEXT_CLASSLOADER_KEY,
     GenReportAutoDs.class.getClassLoader());
   task.setParameterValue("province", 1);
   task.setParameterValue("city", 372);
   HTMLRenderOption options = new HTMLRenderOption();
   options.setOutputFileName(path + "/test/output/cascading"
     + DateUtil.format(new Date(), "HH-mm", false) + ".pdf"); // 生成pdf文件保存在output文件夹里。
   options.setOutputFormat("pdf");
   options.setHtmlRtLFlag(false);
   options.setEmbeddable(false);
   task.setRenderOption(options);
   task.run();
   task.close();
   birtEngine.destroy();
   //System.out.println("关闭引擎……");
   //不可调用,否则报异常
   //Platform.shutdown();
   //System.out.println("关闭平台……");
   System.out.println(DateUtil.format(new Date(), "HH:mm:ss", false)+":包含library报表生成完成!");
  } catch (Exception e) {
   System.out.println("使用库导出报表失败!");
   System.err.println("错误原因:"+e.getMessage());
  }
 }
}
    2、新建quartz配置文件,内容如下:

    <?xml version="1.0" encoding="gbk"?>
<beans xmlns="
http://www.springframework.org/schema/beans"
       xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="
http://www.springframework.org/schema/context"      
       xsi:schemaLocation="
http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context
          
http://www.springframework.org/schema/context/spring-context-2.5.xsd" default-lazy-init="true">

 <bean name="timingSet" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
  <property name="targetObject" ref="genReportAutoDs"></property>
  <property name="targetMethod" value="runReport"></property>
 </bean>
 
 <bean name="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
  <property name="jobDetail" ref="timingSet"></property>
  <property name="cronExpression" value="0 0/1 * * * ?"></property>
 </bean>
 
 <bean name="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean" lazy-init="false">
  <property name="triggers"> 
   <list> 
    <ref bean="cronTrigger"/>
   </list> 
  </property>
  <property name="autoStartup" value="true"/>
 </bean>

</beans>

蓝色标注部分为GenReportAutoDs类对应的bean对象。

     3、启动tomcat服务,可以舒服的看到报表文件按照设定的周期生成啦,心里那个爽歪歪^_^

 

说明:碰到的问题和解决办法:

         1、报表文件无法加载library文件的问题。由于后台调用的java代码无法读取web.xml里的内容,所以需要单独设置资源文件的路径,设置方法为config.setResourcePath(servletContext.getRealPath("/library"));
         2、Platform.shutdown()方法在生成报表后不可调用,否则会产生第一次生成成功,第二次报异常的问题,查了很多资料才找到原因,很崩溃。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Eclipse BIRT(Business Intelligence and Reporting Tools)是一个开源的报表工具,可以帮助开发人员在Eclipse环境下创建和设计报表。 要在Eclipse BIRT中绘制报表,请按照以下步骤操作: 1. 首先,确保您已经安装了Eclipse IDE。如果还没有安装,您可以从Eclipse官方网站(https://www.eclipse.org/downloads/)上下载并安装最新版本的Eclipse。 2. 打开Eclipse IDE并创建一个新的BIRT项目。选择“File” -> “New” -> “Project”,然后在“Project”对话框中选择“Business Intelligence and Reporting Tools” -> “Report Project”。 3. 在创建项目后,在“Project Explorer”视图中,右键单击报表项目并选择“New” -> “Report”。 4. 在“Report Designer”中,您可以使用各种工具和选项来设计报表。您可以添加数据集、数据源、文本框、图表等元素,还可以定义报表布局和样式。 5. 在设计报表时,您可以使用BIRT提供的丰富的功能和选项来自定义报表。您可以设置数据过滤、排序、分组、计算字段等。 6. 在完成报表设计后,您可以通过单击“Preview”按钮预览报表,并根据需要进行调整和修改。 7. 最后,您可以将报表导出为不同的格式(如PDF、Excel、HTML等),或者将其集成到您的应用程序中。 以上是使用Eclipse BIRT绘制报表的简要步骤。您可以根据具体需求进一步学习和探索BIRT的各种功能和选项。祝您成功!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值