不推荐用jsp, 用servlet 比较好,我这里有流程性的,你自己看着用吧,这东西用了2年,没发现问题。
通用的导出类,支持html,Excel,PDF三种
public
class
ReportType
...
{
public static int HTML = 1;
public static int EXCEL = 2;
public static final int PDF = 3;
public static void export(HttpServletResponse response, JasperPrint jp, int type, String filename) throws Exception ...{
JRExporter exporter = null;
if (type == HTML) ...{
exporter = new JRHtmlExporter();
exporter.setParameter(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN, false);
exporter.setParameter(JRHtmlExporterParameter.BETWEEN_PAGES_HTML, "");
// exporter.setParameter(JRHtmlExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE);
} else if (type == EXCEL) ...{
exporter = new JRXlsExporter();
response.setContentType("application/vnd.ms-excel");
response.addHeader("Content-Disposition", new String(("attachment; filename=" + filename + ".xls").getBytes("GBK"), "ISO-8859-1"));
exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE);
// exporter.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE);
} else if (type == PDF) ...{
exporter = new JRPdfExporter();
response.setContentType("application/pdf");
response.addHeader("Content-Disposition", new String(("attachment; filename=" + filename + ".pdf").getBytes("GBK"), "ISO-8859-1"));
} else ...{
return;
}
exporter.setParameter(JRExporterParameter.CHARACTER_ENCODING, "GBK");
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jp);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, response.getOutputStream());
exporter.exportReport();
}
}
public static int HTML = 1;
public static int EXCEL = 2;
public static final int PDF = 3;
public static void export(HttpServletResponse response, JasperPrint jp, int type, String filename) throws Exception ...{
JRExporter exporter = null;
if (type == HTML) ...{
exporter = new JRHtmlExporter();
exporter.setParameter(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN, false);
exporter.setParameter(JRHtmlExporterParameter.BETWEEN_PAGES_HTML, "");
// exporter.setParameter(JRHtmlExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE);
} else if (type == EXCEL) ...{
exporter = new JRXlsExporter();
response.setContentType("application/vnd.ms-excel");
response.addHeader("Content-Disposition", new String(("attachment; filename=" + filename + ".xls").getBytes("GBK"), "ISO-8859-1"));
exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE);
// exporter.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE);
} else if (type == PDF) ...{
exporter = new JRPdfExporter();
response.setContentType("application/pdf");
response.addHeader("Content-Disposition", new String(("attachment; filename=" + filename + ".pdf").getBytes("GBK"), "ISO-8859-1"));
} else ...{
return;
}
exporter.setParameter(JRExporterParameter.CHARACTER_ENCODING, "GBK");
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jp);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, response.getOutputStream());
exporter.exportReport();
}
}
使用方法
String jasper
=
"
jasper/saler/salerMonthSummary.jasper
"
;
//
你的jasper文件地址
String filename = " 营销中心业务员 " + from + " - " + to + " 销售汇总 " ; // 对于excel等需要下载的文件名
// 各种参数设置好
Map map = new HashMap();
map.put( " YearFrom " , from.getYear());
map.put( " MonthFrom " , from.getMonth());
map.put( " YearTo " , to.getYear());
map.put( " MonthTo " , to.getMonth());
// 读取jasper
JasperReport jr = (JasperReport) JRLoader.loadObjectFromLocation(jasper);
// 填充数据
JasperPrint jp = JasperFillManager.fillReport(jr, map, null );
// 判断是否正常
List < JRPrintPage > pages = jp.getPages();
if (pages.size() == 0 ) ... {
// 没有数据
return;
}
ReportType.export(response, jp, ReportType.EXCEL, filename);
String filename = " 营销中心业务员 " + from + " - " + to + " 销售汇总 " ; // 对于excel等需要下载的文件名
// 各种参数设置好
Map map = new HashMap();
map.put( " YearFrom " , from.getYear());
map.put( " MonthFrom " , from.getMonth());
map.put( " YearTo " , to.getYear());
map.put( " MonthTo " , to.getMonth());
// 读取jasper
JasperReport jr = (JasperReport) JRLoader.loadObjectFromLocation(jasper);
// 填充数据
JasperPrint jp = JasperFillManager.fillReport(jr, map, null );
// 判断是否正常
List < JRPrintPage > pages = jp.getPages();
if (pages.size() == 0 ) ... {
// 没有数据
return;
}
ReportType.export(response, jp, ReportType.EXCEL, filename);