展现报表的同时生成rat文件

最近遇到了这样一个客户需求,在报表展现的时候后台自动将报表生成rat文件,这样以后访问的时候就可以直接访问带有数据的rat文件,不用再去计算报表了。下面就用一个小例子来实现在展现报表的时候后台自动生成rat文件到指定的路径。

实现思路:在jsp中计算报表,然后用生成的IReport对象去生成rat文件,然后采用bean的方式发布报表。

在jsp中将报表计算成IReport对象的代码如下:

String reportFileHome=Context.getInitCtx().getMainDir();
String raqPath=application.getRealPath(reportFileHome);
String raq = raqPath+”/test.raq”;
ReportDefine rd = (ReportDefine) ReportUtils.read(raq);
Context cxt = new Context();
Engine engine = new Engine(rd,cxt);
IReport ireport = engine.calc();

这样报表文件计算后的IReport对象就得到了,

下面就用这个IReport对象来生成rat文件到指定的路径。

生成rat文件的API代码如下所示:

OutputStream os = new FileOutputStream(”D:/test.rat”);
ReportUtils.write(os.ireport);

上述代码的含义是先创建一个输出流到本地的D盘根目录下,生成的rat名字指定为test.rat,然后调用ReportUtils的write方法将rat文件输出到本地。

完成上述两个步骤后就利用上面生成的IReport对象用bean的方式发布报表,把IReport对象放到request中:request.setAttribute(”ireport”,ireport);

发布报表的完整jsp如下:

<%@ page contentType=”text/html;charset=GBK”%>
<%@page import=”com.zhengzhong.practise.ConnectDemo”%>
<%@ taglib uri=”/WEB-INF/runqianReport4.tld” prefix=”report”%>
<%@ page import=”com.runqian.report4.model.*”%>
<%@ page import=”com.runqian.report4.usermodel.*”%>
<%@ page import=”com.runqian.report4.util.*”%>
<%@ page import=”java.util.*”%>
<html>
<body topmargin=0 leftmargin=0 rightmargin=0 bottomMargin=0>
<%
String reportFileHome=Context.getInitCtx().getMainDir();
String raqPath=application.getRealPath(reportFileHome);
String raq = raqPath+”/test.raq”;
ReportDefine rd = (ReportDefine) ReportUtils.read(raq);
Context cxt = new Context();
Engine engine = new Engine(rd,cxt);
IReport ireport = engine.calc();
OutputStream os = new FileOutputStream(”D:/test.rat”);
ReportUtils.write(os.ireport);
request.setAttribute(”ireport”,ireport);
%>
<table align=left>
<tr><td>
<!– 发布报表的tag标签,与struts标签使用类似,其中name为必填项,reportFileName与beanName根据发布的报表源类型选择性填写一个 –>
<!– 这里主要讲解了报表源为bean的使用,并在exceptionPage属性里指定了发生错误时,展现错误信息的页面 –>
<report:html name=”report1″
srcType=”defineBean”
beanName=”ireport”
funcBarLocation=”top”
exceptionPage=”/jsp/myError.jsp”
/>
</td></tr>
</table>


</body>
</html>

把上述代码保存成test.jsp,用上述jsp发布报表,可以看到rat文件被输出到D盘的根目录下了,具体的效果如下图所示:

这样,后台自动生成rat文件的需求就实现了。

浏览次数:0 ; 发布日期:2010/08/20

热门文章

  • 申请试用授权 - 49,853 views
  • 使用入门教程简介 - 30,987 views
  • .NET报表产品概况 - 27,730 views
  • 联系我们 - 13,867 views
  • 技术支持 - 10,185 views
  • 高效报表设计 - 9,033 views
  • Web报表工具的新起点 - 7,563 views
  • 复杂报表的制作 - 7,033 views
  • Web报表软件的采购成本 - 6,530 views
  • Web报表软件的集成方案 - 5,075 views