如何将ireport生成的jasper 嵌入到jsp 中
1个回答
展开全部
不推荐用jsp, 用servlet 比较好,我这里有流程性的,你自己看着用吧
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();
}
}
使用方法
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);
但是我觉得finereport会更方便一点,你可以尝试一下哇
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();
}
}
使用方法
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);
但是我觉得finereport会更方便一点,你可以尝试一下哇
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询