第一,二,三,四步请参照:JasperReport+Servlet Web应用之:打印
导PDF文件的Servlet如下:
package com.mengya.servlet;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.export.JRPdfExporter;
import net.sf.jasperreports.engine.util.FileBufferedOutputStream;
import net.sf.jasperreports.engine.util.JRLoader;
import com.mengya.JRDataSource.PersonDataSource;
public class JRPDFServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
ServletContext servletContext = this.getServletConfig()
.getServletContext();
File jasperFile = new File(servletContext
.getRealPath("/jasper/preson.jasper"));
Map<String, String> parameters = new HashMap<String, String>();
parameters.put("year", "2009");
parameters.put("unit_mc", "武汉XX科技有限公司");
JasperPrint jasperPrint = null;
try {
JasperReport jasperReport = (JasperReport) JRLoader
.loadObject(jasperFile.getPath());
jasperPrint = JasperFillManager.fillReport(jasperReport,
parameters, new PersonDataSource());
} catch (JRException e) {
e.printStackTrace();
}
if (null != jasperPrint) {
FileBufferedOutputStream fbos = new FileBufferedOutputStream();
JRPdfExporter exporter = new JRPdfExporter();
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, fbos);
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
try {
exporter.exportReport();
fbos.close();
if (fbos.size() > 0) {
response.setContentType("application/pdf");
response.setContentLength(fbos.size());
ServletOutputStream ouputStream = response.getOutputStream();
try {
fbos.writeData(ouputStream);
fbos.dispose();
ouputStream.flush();
} finally {
if (null != ouputStream) {
ouputStream.close();
}
}
}
} catch (JRException e1) {
e1.printStackTrace();
}finally{
if(null !=fbos){
fbos.close();
fbos.dispose();
}
}
}
}
}
导Excel文件的Servlet如下:
public class JRJXLServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
ServletContext context = this.getServletConfig().getServletContext();
File jasperFile = new File(context.getRealPath("/jasper/preson.jasper"));
Map<String, String> parameters = new HashMap<String, String>();
parameters.put("year", "2009");
parameters.put("unit_mc", "武汉XX科技有限公司");
JasperPrint jasperPrint = null;
try {
JasperReport jasperReport = (JasperReport) JRLoader
.loadObject(jasperFile);
jasperPrint = JasperFillManager.fillReport(jasperReport, parameters,
new PersonDataSource());
} catch (JRException e) {
e.printStackTrace();
}
if(null != jasperPrint){
FileBufferedOutputStream fbos = new FileBufferedOutputStream();
JRXlsAbstractExporter exporter = new JExcelApiExporter();
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, fbos);
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRXlsAbstractExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE);
exporter.setParameter(JRXlsAbstractExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE);
try {
exporter.exportReport();
fbos.close();
if (fbos.size() > 0) {
response.setContentType("application/exl");
response.setHeader("Content-Disposition", "inline; filename=\"persons.xls\"");
response.setContentLength(fbos.size());
ServletOutputStream ouputStream = response.getOutputStream();
try {
fbos.writeData(ouputStream);
fbos.dispose();
ouputStream.flush();
} finally {
if (null != ouputStream) {
ouputStream.close();
}
}
}
} catch (JRException e1) {
e1.printStackTrace();
}finally{
if(null !=fbos){
fbos.close();
fbos.dispose();
}
}
}
}
}
导rtf文件的servlet如下:
public class JRRTFServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
ServletContext context = this.getServletConfig().getServletContext();
File jasperFile = new File(context.getRealPath("/jasper/preson.jasper"));
Map<String, String> parameters = new HashMap<String, String>();
parameters.put("year", "2009");
parameters.put("unit_mc", "武汉XX科技有限公司");
JasperPrint jasperPrint = null;
try {
JasperReport jasperReport = (JasperReport) JRLoader
.loadObject(jasperFile);
jasperPrint = JasperFillManager.fillReport(jasperReport, parameters,
new PersonDataSource());
} catch (JRException e) {
e.printStackTrace();
}
if(null != jasperPrint){
FileBufferedOutputStream fbos = new FileBufferedOutputStream();
JRRtfExporter exporter = new JRRtfExporter();
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, fbos);
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
try {
exporter.exportReport();
fbos.close();
if (fbos.size() > 0) {
response.setContentType("application/rtf");
response.setHeader("Content-Disposition", "inline; filename=\"person.rtf\"");
response.setContentLength(fbos.size());
ServletOutputStream ouputStream = response.getOutputStream();
try {
fbos.writeData(ouputStream);
fbos.dispose();
ouputStream.flush();
} finally {
if (null != ouputStream) {
ouputStream.close();
}
}
}
} catch (JRException e1) {
e1.printStackTrace();
}finally{
if(null !=fbos){
fbos.close();
fbos.dispose();
}
}
}
}
}
该Servlet在web.xml配置如下:
<servlet>
<servlet-name>JRPDFServlet</servlet-name>
<servlet-class>com.mengya.servlet.JRPDFServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>JREXLServlet</servlet-name>
<servlet-class>com.mengya.servlet.JREXLServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>JRRTFServlet</servlet-name>
<servlet-class>com.mengya.servlet.JRRTFServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>JRPDFServlet</servlet-name>
<url-pattern>/JRPDFServlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>JREXLServlet</servlet-name>
<url-pattern>/JREXLServlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>JRRTFServlet</servlet-name>
<url-pattern>/JRRTFServlet</url-pattern>
</servlet-mapping>
页面调用如下:
h3><a href="JRPDFServlet">导出PDF文件</a></h3>
<br>
<h3><a href="JREXLServlet">导出EXL文件</a></h3>
<br>
<h3><a href="JRRTFServlet">导出RTF文件</a></h3>
<br>
分享到:
相关推荐
**JasperReport与Servlet在Web应用中的打印预览** JasperReport是一款强大的开源报表工具,它允许开发者在Java应用程序、Web应用甚至独立的Java应用程序中创建复杂的报告。Servlet是Java Web开发中的一个重要组件,...
标题"JasperReport+Servlet Web应用之:打印"表明这是一个关于如何在基于Servlet的Web应用程序中使用JasperReport进行报表打印的教程。JasperReport是一款强大的开源报表工具,它允许开发者创建复杂的报表并将其集成...
在Web应用中,Servlet常用于接收和处理用户的请求,而JasperReport则可以作为Servlet的后端服务,生成报表内容。 要实现HTML显示报表,我们需要完成以下步骤: 1. **报表模板设计**:使用JasperReport提供的设计...
在“Struts2 + JasperReport应用一:导PDF,Excel,HTML显示”这篇博文中,作者将介绍如何结合这两个工具来实现动态报表的生成和导出。 首先,我们需要了解Struts2的基础知识。Struts2框架是Apache软件基金会的一个...
在"使用JasperReport+iReport进行WEB开发"的实践中,首先需要安装并配置iReport,下载并解压后,可以通过它来设计报表模板。模板设计完成后,将其保存为.jrxml文件,这是JasperReport的XML格式,包含了报表的所有...
JasperReport和iReport是Java世界中常用的报表生成工具,它们可以帮助开发者轻松创建复杂的报表,并在Web应用程序中集成。在JasperReport 2.0.2和iReport 2.0.2版本中,我们可以实现多种功能,包括字段、变量、参数...
### JasperReport+iReport 报表开发手册核心知识点详解 #### 一、JasperReport与iReport概述 **JasperReport** 是一款强大的、灵活的报表生成工具,它能够展示丰富的页面内容,并支持将这些内容转换为PDF、HTML或...
**jasperReport+iReport制作PDF报表步骤** 1. **安装iReport**:首先需要下载并安装iReport工具,设置好JDK环境。 2. **创建数据源**:在iReport中配置数据源,如数据库连接,确保报表能够获取到数据。 3. **设计...
### JasperReport+iReport整合的Web报表开发 #### 1. JasperReport简介 JasperReport是一款功能强大且灵活的报表生成工具,它支持多种输出格式,包括PDF、HTML、XML、Excel(通过POI或JExcelAPI实现)和Rtf(通过...
1. 加载报表模板:使用 JasperReport 类的 compileReport 方法将 .jrxml 文件编译为 .jasper 文件。 2. 数据填充:通过 JasperFillManager 的 fillReport 方法,用实际数据填充报表模板。 3. 报表导出:使用 ...
jasperreport+ireport+jfreechart文档及代码1 jasperreport+ireport+jfreechart文档及代码1 jasperreport+ireport+jfreechart文档及代码1
jasperreport+ireport+jfreechart文档及代码2 jasperreport+ireport+jfreechart文档及代码2 jasperreport+ireport+jfreechart文档及代码2
7. **jasperreport+ireport开发java报表入门级教程.pdf**:专门针对Java开发者的入门教程,详细解释如何使用JasperReport和IReport构建报表。 8. **jasperReport.pdf**:可能是一个JasperReport的官方文档或者第三方...