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()方法在生成报表后不可调用,否则会产生第一次生成成功,第二次报异常的问题,查了很多资料才找到原因,很崩溃。