报表应用中有时报表只需生成指定文件,而无需在页面展现,生成的文件常见于Excel、PDF等。由于报表不展现,也就无需使用报表提供的tag,可以直接通过程序导出。集算报表提供了丰富的API,允许开发人员编写代码实现自己的业务逻辑。这里通过在servlet中导出Excel为例来看一下报表不展现而直接导出的实现方式。
定义导出Excel的servlet(ExportToExcel),主要代码如下:
1、接收参数
String report =request.getParameter("rpx");
String year = request.getParameter("year");
2、读入报表
String reportFile =request.getSession().getServletContext().getRealPath("/reportFiles/"+report);
3、读入并计算报表
Context cxt = new Context();
ReportDefine rd =(ReportDefine)ReportUtils.read(reportFile);
cxt.setParamValue("year", year);
Engine engine = new Engine(rd, cxt);
IReport iReport = engine.calc();
4、导出Excel
ReportUtils.exportToExcel("D:/result.xls",iReport, true);
根据页面超链接访问上述servlet,并传递参数,如:
http://192.168.1.102:8080/rpx_demo/servlet/ExportToExcel?rpx=order.rpx&year=1996即可直接生成Excel文件,而无需展现报表。
导出的Excel效果如下:
集算报表提供了多个导出Excel的方法,除了可以导出xls格式,也可以导出xlsx格式文件,开发人员可以根据需求选择导出Excel的方法:
ReportUtils.exportToExcel(java.io.OutputStream os, IReport report,boolean pageBroken);
ReportUtils.exportToExcel(java.io.OutputStream os, IReport report,boolean pageBroken, int dispRatio);
ReportUtils.exportToExcel(java.lang.String fileName, IReport report,boolean pageBroken);
ReportUtils.exportToExcel(java.lang.String fileName, IReport report,boolean pageBroken, int dispRatio);
ReportUtils.exportToExcel 2007(java.lang.String fileName,IReportreport,boolean pageBroken)
ReportUtils.exportToExcel 2007(java.io.OutputStream os,IReportreport,boolean pageBroken)
导出PDF与导出Excel类似,调用相应导出PDF的方法即可,集算报表提供导出PDF的方法包括:
ReportUtils.exportToPDF(java.io.OutputStream os, IReport report);
ReportUtils.exportToPDF(java.io.OutputStream os, IReport report,boolean pageBroken, boolean graphicOut)
ReportUtils.exportToPDF(java.lang.String fileName, IReport report);
ReportUtils.exportToPDF(java.lang.String fileName, IReport report,boolean pageBroken, boolean graphicOut)
【附】导出Excel完整代码
package com.runqian.servlet; import java.io.File; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.raqsoft.report.model.ReportDefine; import com.raqsoft.report.usermodel.Context; import com.raqsoft.report.usermodel.Engine; import com.raqsoft.report.usermodel.IReport; import com.raqsoft.report.usermodel.ParamMetaData; import com.raqsoft.report.util.ReportUtils; publicclassExportToExcel extends HttpServlet { /** * Constructor of the object. */ public ExportToExcel() { super(); } /** * Destruction of the servlet. <br> */ publicvoid destroy() { super.destroy(); // Just puts "destroy" string in log // Put your code here } /** * The doGet method of the servlet. <br> * * This method is called when a form has itstag value method equals to get. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ publicvoiddoGet(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException { Stringreport = request.getParameter("rpx"); Stringyear = request.getParameter("year"); //保证报表名称的完整性 int iTmp = 0; if( (iTmp =report.lastIndexOf(".rpx")) <= 0 ){ report= report + ".rpx"; iTmp= 0; } StringreportFile = request.getSession().getServletContext().getRealPath("/reportFiles/"+report); Contextcxt = newContext(); try { ReportDefinerd = (ReportDefine)ReportUtils.read(reportFile); cxt.setParamValue("year", year); Engineengine = newEngine(rd, cxt); //构造报表引擎 IReportiReport = engine.calc(); //运算报表 ReportUtils.exportToExcel("D:\\result.xls",iReport, true); }catch(Throwable e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * The doPost method of the servlet. <br> * * This method is called when a form has itstag value method equals to post. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ publicvoiddoPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException { response.setContentType("text/html"); PrintWriterout = response.getWriter(); out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML4.01 Transitional//EN\">"); out.println("<HTML>"); out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>"); out.println(" <BODY>"); out.print(" This is"); out.print(this.getClass()); out.println(", using the POST method"); out.println(" </BODY>"); out.println("</HTML>"); out.flush(); out.close(); } /** * Initialization of the servlet. <br> * * @throws ServletException if an error occurs */ publicvoid init() throws ServletException { // Put your code here } }
本文出自 “高性能报表数据计算” 博客,请务必保留此出处http://report5.blog.51cto.com/8028595/1635941
原文地址:http://report5.blog.51cto.com/8028595/1635941