1.数据源(JRDataSource)
import java.util.ArrayList;
import java.util.List;
import lbc.report.basequery.QueryData;
import lbc.report.bean.VioWritJycfjds;
import lbc.report.bean.base.BaseVioWritJycfjds;
import org.hibernate.Session;
import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRField;
/**
* @类名:ReportDataSource.java
* @类功能描述: 产生报表填充需要使用的数据源
* @author lbc
* @version v1.0.0.1
* @see
*/
public class ReportDataSource implements JRDataSource {
private int index = -1;
private static QueryData queryData=new QueryData();
private static List list=new ArrayList();
static
{
String SQL="select * from VIO_WRIT_JYCFJDS";
list=queryData.sqlQuery(SQL, VioWritJycfjds.class);
}
public Object getFieldValue(JRField field) throws JRException {
String fieldName=field.getName();
if(fieldName.equals("bh"))
{
return ((VioWritJycfjds)list.get(index)).getId();
}
if(fieldName.equals("bt"))
{
return ((VioWritJycfjds)list.get(index)).getJybt();
}
if(fieldName.equals("sfjg") && (((VioWritJycfjds) list.get(index)).getJysfjg())!=null && (((VioWritJycfjds) list.get(index)).getJysfjg()).equals("1"))
{
return "/u221A";
}
}
2.JasperPrintWithJRDataSource类
import java.io.File;
import java.util.Map;
import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.util.JRLoader;
/**
* @类名:JasperPrintWithJRDataSource.java
* @类功能描述: 使用报表模板及数据等生成JasperPrint
* @author lbc
* @version v1.0.0.1
* @see
*/
/**
* 使用报表模板及数据等生成JasperPrint
* */
public class JasperPrintWithJRDataSource {
/**传入的参数**/
private Map params;
/**jrxml文件**/
private String jrxmlFilePath;
/**模板文件的地址**/
private String reportFilePath;
/**JRDataSource**/
private JRDataSource dataSource;
public JRDataSource getDataSource() {
return dataSource;
}
public void setDataSource(JRDataSource dataSource) {
this.dataSource = dataSource;
}
public String getJrxmlFilePath() {
return jrxmlFilePath;
}
public void setJrxmlFilePath(String jrxmlFilePath) {
this.jrxmlFilePath = jrxmlFilePath;
}
public Map getParams() {
return params;
}
public void setParams(Map params) {
this.params = params;
}
public String getReportFilePath() {
return reportFilePath;
}
public void setReportFilePath(String reportFilePath) throws Exception {
if(reportFilePath==null || !reportFilePath.endsWith(".jasper"))
throw new Exception("传入的模板文件格式不对,请传入以.jasper为后缀的文件!");
this.reportFilePath = reportFilePath;
}
public JasperPrintWithJRDataSource () {
super();
}
public JasperPrintWithJRDataSource(String jrxmlFilePath,String reportFilePath,Map params,JRDataSource dataSource) throws Exception {
if(jrxmlFilePath==null || !jrxmlFilePath.endsWith(".jrxml"))
throw new Exception("模板文件格式不对,请传入以.jrxml为后缀的文件!");
if(dataSource==null)
throw new Exception("dataSource不应当为null!");
this.setJrxmlFilePath(jrxmlFilePath);
this.setReportFilePath(reportFilePath);
this.setParams(params);
this.setDataSource(dataSource);
}
/**
* 取得JasperPrint
*/
public JasperPrint getJasperPrint() throws Exception {
//编译成.Jasper文件
JasperCompileManager.compileReportToFile(this.jrxmlFilePath);
File reportFile=new File(this.reportFilePath);
if(!reportFile.exists())
throw new Exception("传入的模板文件不存在!");
try {
//LOAD 编译好的模板
JasperReport jasperReport=(JasperReport) JRLoader.loadObject(reportFile.getPath());
//进行数据填充
JasperPrint jasperPrint=JasperFillManager.fillReport(jasperReport, this.params, this.dataSource);
return jasperPrint;
} catch (JRException jre) {
jre.printStackTrace();
throw new Exception("数据填充发生错误,请检查数据库源和用来填充的参数map是否有误!");
}
}
}
3.导出HTML格式报表
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspWriter;
import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.export.JRHtmlExporter;
import net.sf.jasperreports.engine.export.JRHtmlExporterParameter;
import net.sf.jasperreports.j2ee.servlets.ImageServlet;
/**
* @类名:HTMLExport.java
* @类功能描述: 利用报表生成HTML格式报表
* @author lbc
* @version v1.0.0.1
* @see
*/
/*
* 利用报表生成HTML格式报表
*/
public class HTMLExport {
/*
* 导出报表
*/
public void export(HttpServletRequest request,HttpServletResponse response,String jrxmlFilePath,String reportFilePath,Map params,JRDataSource dataSource) throws Exception {
try {
response.reset();
response.setContentType("text/html; charset=GBK");
PrintWriter writer=response.getWriter();
try {
JasperPrint jasperPrint=new JasperPrintWithJRDataSourceNew(jrxmlFilePath,reportFilePath,params,dataSource).getJasperPrint();
//使用JRHtmlExporter导出Html格式
JRHtmlExporter exporter=new JRHtmlExporter();
request.getSession().setAttribute(ImageServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, jasperPrint);
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_WRITER, writer);
//报表边框图片设置"report/image?image=",report为你的报表及PX图片所在目录
//exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI, "report/image?image=");
//报表边框图片设置IS_USING_IMAGES_TO_ALIGN,Boolean.FALSE,不使用图片
exporter.setParameter(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN,Boolean.FALSE);
exporter.setParameter(JRExporterParameter.CHARACTER_ENCODING, "GBK");
//导出
exporter.exportReport();
} catch (Exception e) {
e.printStackTrace();
throw new Exception("导出HTML格式报表时发生错误!");
}finally{
if(writer!=null)
{
try {
writer.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
} catch (IOException ioe) {
ioe.printStackTrace();
}
throw new Exception("使用JRHtmlExporter导出Html格式时发生错误!");
}
4.导出Pdf报表
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletOutputStream;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperRunManager;
import net.sf.jasperreports.engine.export.JRHtmlExporterParameter;
import net.sf.jasperreports.engine.export.JRPdfExporter;
import net.sf.jasperreports.engine.export.JRPdfExporterParameter;
/**
* @类名:PdfExport.java
* @类功能描述: 导出PDF报表
* @author lbc
* @version v1.0.0.1
* @see
*/
public class PDFExport {
public void exportToPDF(HttpServletRequest request,HttpServletResponse response,String jrxmlFilePath,String reportFilePath,Map params,JRDataSource dataSource) throws Exception
{
try{
response.reset();
response.setContentType("application/pdf;charset=GBK");
//得到jasperPrint对象
JasperPrint jasperPrint=new JasperPrintWithJRDataSourceNew(jrxmlFilePath,reportFilePath,params,dataSource).getJasperPrint();
JRPdfExporter pdfexporter = new JRPdfExporter();
pdfexporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
//设置输出流
pdfexporter.setParameter(JRExporterParameter.OUTPUT_STREAM, response.getOutputStream());
//如果保存到硬盘用下面语句
//pdfexporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, "c://mypdf.pdf");
pdfexporter.setParameter(JRPdfExporterParameter.IS_ENCRYPTED, Boolean.FALSE);
pdfexporter.setParameter(JRExporterParameter.CHARACTER_ENCODING, "GBK");
pdfexporter.exportReport();
System.out.println("Done!");
}catch(IOException e){
e.printStackTrace();
}
}
}
5.导出RTF报表
import java.io.IOException;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspWriter;
import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.export.JRRtfExporter;
/**
* @类名:RTFExport.java
* @类功能描述:导出RTF报表
* @author lbc
* @version v1.0.0.1
* @see
*/
public class RTFExport {
public void exportToRtf(HttpServletRequest request,HttpServletResponse response,String jrxmlFilePath,String reportFilePath,Map params,JRDataSource dataSource) throws Exception {
try {
JasperPrint jasperPrint=new JasperPrintWithJRDataSourceNew(jrxmlFilePath,reportFilePath,params,dataSource).getJasperPrint();
JRRtfExporter rtfExporter=new JRRtfExporter();
rtfExporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
//设置导出文件名
rtfExporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, "c://myrtf.rtf");
rtfExporter.setParameter(JRExporterParameter.CHARACTER_ENCODING, "GB2312");
rtfExporter.exportReport();
System.out.println("Done!");
} catch (IOException e) {
e.printStackTrace();
}
}
}
6.导出XML报表
import java.io.IOException;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.export.JRXmlExporter;
/**
* @类名:XMLExport.java
* @类功能描述: 导出XML报表
* @author lbc
* @version v1.0.0.1
* @see
*/
public class XMLExport {
public void exportToXml(HttpServletRequest request,HttpServletResponse response,String jrxmlFilePath,String reportFilePath,Map params,JRDataSource dataSource) throws Exception {
try {
JasperPrint jasperPrint=new JasperPrintWithJRDataSourceNew(jrxmlFilePath,reportFilePath,params,dataSource).getJasperPrint();
JRXmlExporter xmlExporter=new JRXmlExporter();
xmlExporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
xmlExporter.setParameter(JRExporterParameter.OUTPUT_STREAM, response.getOutputStream());
xmlExporter.exportReport();
response.setContentType("text/xml");
System.out.println("Done!");
} catch (IOException e) {
e.printStackTrace();
}
}
}
由于在浏览器中打开Xml时,在页面设置里需进行内容类型的设置,因此最好在JSP页面中加上如下代码:
<%@ page contentType="text/xml;charset=GBK"%>
7.导出Excel报表
import java.io.IOException;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.export.JRXlsExporter;
import net.sf.jasperreports.engine.export.JRXlsExporterParameter;
/**
* @类名:EXCELExport.java
* @类功能描述: 导出Excel报表
* @author lbc
* @version v1.0.0.1
* @see
*/
public class EXCELExport {
public void exportToExcel(HttpServletRequest request,HttpServletResponse response,String jrxmlFilePath,String reportFilePath,Map params,JRDataSource dataSource) throws Exception {
try {
response.reset();
response.setContentType("application/vnd.ms-excel;charset=GBK");
JasperPrint jasperPrint=new JasperPrintWithJRDataSourceNew(jrxmlFilePath,reportFilePath,params,dataSource).getJasperPrint();
JRXlsExporter jrXlsExporter=new JRXlsExporter();
jrXlsExporter.setParameter(JRExporterParameter.JASPER_PRINT,jasperPrint);
//设置输出流
jrXlsExporter.setParameter(JRExporterParameter.OUTPUT_STREAM,response.getOutputStream());
jrXlsExporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET,Boolean.FALSE);
jrXlsExporter.setParameter(JRExporterParameter.CHARACTER_ENCODING, "GB2312");
jrXlsExporter.exportReport();
System.out.println("Done!");
} catch (IOException e) {
e.printStackTrace();
}
}
}
由于在浏览器中打开Excel时,在页面设置里需进行内容类型的设置,因此最好在JSP页面中加上如下代码:
<%@ page contentType="application/vnd.ms-excel;charset=GBK"%>
8.ACTION调用,导出报表
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import lbc.report.report.EXCELExport;
import lbc.report.report.HTMLExportNew;
import lbc.report.report.PDFExport;
import lbc.report.report.RTFExport;
import lbc.report.report.ReportDataSource;
import lbc.report.report.XMLExport;
import net.sf.jasperreports.engine.JRDataSource;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
/**
* @类名:jycfjdsAction.java
* @类功能描述: ACTION调用,导出报表
* @author lbc
* @version v1.0.0.1
* @see
*/
public class jycfjdsAction extends DispatchAction {
public ActionForward loadReport(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws Exception {
Map params = new HashMap();
JRDataSource dataSource = new ReportDataSource();
//导出HTML报表
HTMLExportNew htmExportNew=new HTMLExportNew();
htmExportNew.export(request,response,request.getRealPath("/report/jycfjds.jrxml"), request.getRealPath("/report/jycfjds.jasper"), params, dataSource);
//导出PDF报表
//PDFExport pdfExport=new PDFExport();
//pdfExport.exportToPDF(request, response, request.getRealPath("/report/jycfjds.jrxml"),request.getRealPath("/report/jycfjds.jasper"), params, dataSource);
//导出EXCEL报表
//EXCELExport excelExport=new EXCELExport();
//excelExport.exportToExcel(request, response, request.getRealPath("/report/jycfjds.jrxml"), request.getRealPath("/report/jycfjds.jasper"), params, dataSource);
//导出XML报表
//XMLExport xmlExport=new XMLExport();
//xmlExport.exportToXml(request, response, request.getRealPath("/report/jycfjds.jrxml"), request.getRealPath("/report/jycfjds.jasper"), params, dataSource);
//导出RTF报表
//RTFExport rtfExport=new RTFExport();
//rtfExport.exportToRtf(request, response,request.getRealPath("/report/jycfjds.jrxml"), request.getRealPath("/report/jycfjds.jasper"), params, dataSource);
return null;
}
}
9.调用JSP页面导出报表
<%@ page language="java" contentType="text/html; charset=GBK" pageEncoding="GBK"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@page import="lbc.report.basequery.QueryData"%>
<%@page import="java.util.List"%>
<%@page import="java.util.ArrayList"%>
<%@page import="lbc.report.report.ReportDataSource"%>
<%@page import="java.util.HashMap"%>
<%@page import="java.util.Map"%>
<%@page import="lbc.report.report.HTMLExportNew"%>
<%@page import="lbc.report.report.PDFExport"%>
<%@page import="net.sf.jasperreports.engine.JRDataSource"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>导出HTML格式报表</title>
</head>
<body>
<%
//设置JRDataSource连接
JRDataSource dataSource=new ReportDataSource();
//设置外部参数对
Map parameters=new HashMap();
parameters.put("rptToday","2009-06-04");
parameters.put("rptMan","lbc");
PDFExport pdfExport=new PDFExport();
pdfExport.exportToPDF(request,response,request.getRealPath("/reports/jycfjds.jrxml"),request.getRealPath("/reports/jycfjds.jasper"),parameters,dataSource);
%>
</body>
</html>