知识库

推荐内容

快逸做的更好

产品优势

您的位置: 首页 > 知识库 > 展现报表的同时生成rat文件

展现报表的同时生成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 = (ReportDefineReportUtils.read(raq);

Context cxt = new Context();

Engine engine = new Engine(rd,cxt);

IReport ireport = engine.calc();

这样报表文件计算后的IReport对象就得到了,下面就用这个IReport对象来生成rat文件到指定的路径。

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

OutputStream os = new FileOutputStreamD:/test.rat;

ReportUtils.write(os.ireport);

上述代码的含义是先创建一个输出流到本地的D盘根目录下,生成的rat名字指定为test.rat,然后调用ReportUtilswrite方法将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 = (ReportDefineReportUtils.read(raq);

Context cxt = new Context();

Engine engine = new Engine(rd,cxt);

IReport ireport = engine.calc();

OutputStream os = new FileOutputStreamD:/test.rat;

ReportUtils.write(os.ireport);

request.setAttribute(ireport”,ireport);

%>

<table align=left>

<tr><td>

<!– 发布报表的tag标签,与struts标签使用类似,其中name为必填项,reportFileNamebeanName根据发布的报表源类型选择性填写一个 –>

<!– 这里主要讲解了报表源为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文件的需求就实现了。

本文标签:
发布日期:2013/04/23
本文分类: 知识库