JasperReport导出工具类
package com.fsm.util;
import java.io.File;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletContext;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.fsm.config.ConfigInfo;
import com.fsm.db.DBConn;
import com.fsm.db.DBService;
import net.sf.jasperreports.engine.*;
import net.sf.jasperreports.engine.export.JRHtmlExporter;
import net.sf.jasperreports.engine.export.JRHtmlExporterParameter;
import net.sf.jasperreports.engine.export.JRPdfExporter;
import net.sf.jasperreports.engine.export.JRXlsExporter;
import net.sf.jasperreports.engine.export.JRXlsExporterParameter;
import net.sf.jasperreports.engine.export.ooxml.JRDocxExporter;
import net.sf.jasperreports.engine.util.JRLoader;
public class ReportUtil {
private HttpServletRequest request;
private HttpServletResponse response;
private Connection conn;
private String strReport;
Map<String, Object> params = new HashMap<String, Object>();
public ReportUtil(HttpServletRequest request, HttpServletResponse response) {
this.request = request;
this.response = response;
strReport = request.getParameter("Report");
strReport = strReport != null ? strReport : "";
String strSessionParam = (String)request.getSession().getAttribute("Report");
strSessionParam = strSessionParam != null ? strSessionParam : "";
if (!strSessionParam.equals("")) {
strReport += ";;" + strSessionParam;
}
String arr[] = strReport.split(";;");
strReport = arr[0];
if (arr.length > 1) {
for (int i = 1; i < arr.length; i++) {
String key = arr[i].split("=")[0];
String value = arr[i].split("=")[1];
try {
value = URLDecoder.decode(value, "UTF-8");
System.out.println("-" + value);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
params.put(key, value);
}
}
ConfigInfo configinfo = new ConfigInfo();
String strWorkpath = configinfo.getPropertiesValue("parameter", "workpath");
strReport = strWorkpath + strReport;
try {
conn = getConnection();
} catch (Exception e) {
e.printStackTrace();
}
}
public static Connection getConnection() throws ClassNotFoundException, SQLException {
DBConn localDBConn = new DBConn();
ConfigInfo localConfigInfo = new ConfigInfo();
String strDBName = localConfigInfo.getPropertiesValue("parameter", "db");
Connection conn = null;
try {
localDBConn.init(strDBName);
conn = localDBConn.getConnection();
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
public void exportHtml() {
try {
response.setCharacterEncoding("UTF-8");
JasperReport jasperReport = (JasperReport) JRLoader.loadObject(strReport);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, conn);
JRHtmlExporter exporter = new JRHtmlExporter();
exporter.setParameter(JRHtmlExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRHtmlExporterParameter.OUTPUT_WRITER, response.getWriter());
exporter.setParameter(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN, Boolean.FALSE);
exporter.setParameter(JRExporterParameter.CHARACTER_ENCODING, "utf-8");
exporter.exportReport();
} catch (JRException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void exportExcel() {
try {
response.setCharacterEncoding("UTF-8");
JasperReport jasperReport = (JasperReport) JRLoader.loadObject(strReport);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, conn);
JRXlsExporter exporter = new JRXlsExporter();
exporter.setParameter(JRXlsExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRXlsExporterParameter.OUTPUT_STREAM, response.getOutputStream());
exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE);
exporter.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE);
response.setHeader("Content-Disposition", "attachment;filename=first.xls");
response.setContentType("application/vnd_ms-excel");
exporter.exportReport();
} catch (JRException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void exportWord() {
try {
response.setCharacterEncoding("UTF-8");
JasperReport jasperReport = (JasperReport) JRLoader.loadObject(strReport);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, conn);
JRAbstractExporter exporter = new JRDocxExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, response.getOutputStream());
response.setHeader("Content-Disposition", "attachment;filename=first.doc");
response.setContentType("application/msword");
exporter.exportReport();
} catch (JRException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void exportPrint() {
try {
response.setCharacterEncoding("UTF-8");
JasperReport jasperReport = (JasperReport) JRLoader.loadObject(strReport);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, conn);
response.setContentType("application/octet-stream");
ServletOutputStream ouputStream = response.getOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(ouputStream);
oos.writeObject(jasperPrint);
oos.flush();
oos.close();
ouputStream.flush();
ouputStream.close();
} catch (JRException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void exportPdf() {
try {
JasperReport jasperReport = (JasperReport) JRLoader.loadObject(strReport);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, conn);
JRPdfExporter exporter = new JRPdfExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, response.getOutputStream());
response.setHeader("Content-Disposition", "attachment;filename=first.pdf");
response.setContentType("application/pdf");
response.setCharacterEncoding("UTF-8");
exporter.exportReport();
} catch (Exception e) {
e.printStackTrace();
}
}
public void export() {
String strReportType = request.getParameter("ReportType");
strReportType = strReportType != null ? strReportType : "";
if (strReportType.equals("html")) {
exportHtml();
} else if (strReportType.equals("word")) {
exportWord();
} else if (strReportType.equals("excel")) {
exportExcel();
} else if (strReportType.equals("pdf")) {
exportPdf();
} else if (strReportType.equals("print")) {
exportPrint();
} else {
exportHtml();
}
}
}