1.数据源(JRDataSource)
imp
imp
imp
imp
imp
imp
imp
imp
imp
/**
* @类名:ReportDataSource.java
* @类功能描述: 产生报表填充需要使用的数据源
* @author lbc
* @version v1.0.0.1
* @see
*/
p lic class ReportDataSource implements JRDataSource {
private int index = -1;
private static QryData qryData=new QryData();
private static List list=new ArrayList();
static
{
String SQL="select * from VIO_WRIT_JYCFJDS";
list=qryData.sqlQry(SQL, VioWritJycfjds.class);
}
p lic Object getFieldVal(JRField field) throws JRException {
String fieldName=field.getName();
if(fieldName.eq ls("bh"))
{
return ((VioWritJycfjds)list.get(index)).getId();
}
if(fieldName.eq ls("bt"))
{
return ((VioWritJycfjds)list.get(index)).getJybt();
}
if(fieldName.eq ls("sfjg") && (((VioWritJycfjds) list.get(index)).getJysfjg())!=null && (((VioWritJycfjds) list.get(index)).getJysfjg()).eq ls("1"))
{
return "\√";
}
}
2.JasperPrintWithJRDataSource类
imp
imp
imp
imp
imp
imp
imp
imp
imp
/**
* @类名:JasperPrintWithJRDataSource.java
* @类功能描述: 使用报表模板及数据等生成JasperPrint
* @author lbc
* @version v1.0.0.1
* @see
*/
/**
* 使用报表模板及数据等生成JasperPrint
* */
p lic class JasperPrintWithJRDataSource {
/**传入的参数**/
private Map params;
/**jrxml文件**/
private String jrxmlFilePath;
/** 模板文件的地址**/
private String reportFilePath;
/**JRDataSource**/
private JRDataSource dataSource;
p lic JRDataSource getDataSource() {
return dataSource;
}
p lic void setDataSource(JRDataSource dataSource) {
this.dataSource = dataSource;
}
p lic String getJrxmlFilePath() {
return jrxmlFilePath;
}
p lic void setJrxmlFilePath(String jrxmlFilePath) {
this.jrxmlFilePath = jrxmlFilePath;
}
p lic Map getParams() {
return params;
}
p lic void setParams(Map params) {
this.params = params;
}
p lic String getReportFilePath() {
return reportFilePath;
}
p lic void setReportFilePath(String reportFilePath) throws Exception {
if(reportFilePath==null || !reportFilePath.endsWith(".jasper"))
throw new Exception("传入的模板文件格式不对,请传入以.jasper为后缀的文件!");
this.reportFilePath = reportFilePath;
}
p lic JasperPrintWithJRDataSource () {
super();
}
p lic 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
*/
p lic 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格式报表
imp
imp
imp
imp
imp
imp
imp
imp
imp
imp
imp
imp
/**
* @类名:HTMLExport.java
* @类功能描述: 利用报表生成HTML格式报表
* @author lbc
* @version v1.0.0.1
* @see
*/
/*
* 利用报表生成HTML格式报表
*/
p lic class HTMLExport {
/*
* 导出报表
*/
p lic void export(HttpServletReqst reqst,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();
reqst.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报表
imp
imp
imp
imp
imp
imp
imp
imp
imp
imp
imp
imp
imp
imp
imp
imp
/**
* @类名:PdfExport.java
* @类功能描述: 导出PDF报表
* @author lbc
* @version v1.0.0.1
* @see
*/
p lic class PDFExport {
p lic void exportToPDF(HttpServletReqst reqst,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报表
imp
imp
imp
imp
imp
imp
imp
imp
imp
/**
* @类名:RTFExport.java
* @类功能描述:导出RTF报表
* @author lbc
* @version v1.0.0.1
* @see
*/
p lic class RTFExport {
p lic void exportToRtf(HttpServletReqst reqst,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报表
imp
imp
imp
imp
imp
imp
imp
imp
/**
* @类名:XMLExport.java
* @类功能描述: 导出XML报表
* @author lbc
* @version v1.0.0.1
* @see
*/
p lic class XMLExport {
p lic void exportToXml(HttpServletReqst reqst,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页面中加上如下代码:
7.导出Excel报表
imp
imp
imp
imp
imp
imp
imp
imp
imp
/**
* @类名:EXCELExport.java
* @类功能描述: 导出Excel报表
* @author lbc
* @version v1.0.0.1
* @see
*/
p lic class EXCELExport {
p lic void exportToExcel(HttpServletReqst reqst,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_ON
jrXlsExporter.setParameter(JRExporterParameter.CHARACTER_ENCODING, "GB2312");
jrXlsExporter.exportReport();
System.out.println("Done!");
} catch (IOException e) {
e.printStackTrace();
}
}
}
由于在浏览器中打开Excel时,在页面设置里需进行内容类型的设置,因此最好在JSP页面中加上如下代码:
8.ACT
imp
imp
imp
imp
imp
imp
imp
imp
imp
imp
imp
imp
imp
imp
imp
/**
* @类名:jycfjdsAction.java
* @类功能描述: ACT
* @author lbc
* @version v1.0.0.1
* @see
*/
p lic class jycfjdsAction extends DispatchAction {
p lic ActionForward loadReport(ActionMapping mapping, ActionForm form,
HttpServletReqst reqst, HttpServletResponse response) throws Exception {
Map params = new HashMap();
JRDataSource dataSource = new ReportDataSource();
//导出HTML报表
HTMLExportNew htmExportNew=new HTMLExportNew();
htmExportNew.export(reqst,response,reqst.getRealPath("/report/jycfjds.jrxml"), reqst.getRealPath("/report/jycfjds.jasper"), params, dataSource);
// 导出PDF报表
//PDFExport pdfExport=new PDFExport();
//pdfExport.exportToPDF(reqst, response, reqst.getRealPath("/report/jycfjds.jrxml"),reqst.getRealPath("/report/jycfjds.jasper"), params, dataSource);
//导出EXCEL报表
//EXCELExport excelExport=new EXCELExport();
//excelExport.exportToExcel(reqst, response, reqst.getRealPath("/report/jycfjds.jrxml"), reqst.getRealPath("/report/jycfjds.jasper"), params, dataSource);
// 导出XML报表
//XMLExport xmlExport=new XMLExport();
//xmlExport.exportToXml(reqst, response, reqst.getRealPath("/report/jycfjds.jrxml"), reqst.getRealPath("/report/jycfjds.jasper"), params, dataSource);
// 导出RTF报表
//RTFExport rtfExport=new RTFExport();
//rtfExport.exportToRtf(reqst, response,reqst.getRealPath("/report/jycfjds.jrxml"), reqst.getRealPath("/report/jycfjds.jasper"), params, dataSource);
return null;
}
}
9.调用JSP页面导出报表
< a href="mailto:%@page" >%@page imp
< a href="mailto:%@page" >%@page imp
< a href="mailto:%@page" >%@page imp
< a href="mailto:%@page" >%@page imp
< a href="mailto:%@page" >%@page imp
< a href="mailto:%@page" >%@page imp
< a href="mailto:%@page" >%@page imp
< a href="mailto:%@page" >%@page imp