润乾报表应用开发之直接导出 Excel/PDF 文件
报表应用中有时报表只需生成指定文件,而无需在页面展现,生成的文件常见于 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
}
}
点击
servletexcel.zip
进行示例下载
用
ReportUtils.read(is)
读入文件时,系统报错,报错内容:java.lang.NoClassDefFoundError: com/raqsoft/common/ICloneable我使用的 java 版本是 jdk1.8.0_231
检查下是否引入了 dm.jar?
导出 Word 怎么弄
输出报表到 Word
ReportUtils. exportToDOCX (java.io.OutputStream os, IReport report);
ReportUtils. exportToDOCX (java.lang.String fileName,IReport report);
改成导出 word 的 api 方法就可以了
示例刷新 url 没有跳转下载是为啥
检查处理类里文件读取和写入的相关代码,确保文件在对应路径上
这块可以修改成自己的处理逻辑的
666