关键代码:
Connection conn = null;
String type = request.getParameter("type");
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection(
"jdbc:oracle:thin:@127.0.0.1:1521:orcl","mutouyihao","xx");
ServletContext servletContext = this.getServletContext();
File reportFile = new File(servletContext.getRealPath("/")+"/WEB-INF/report/Untitled_report_1.jasper");
Map parameters = new HashMap();
if("pdf".equals(type)){
byte[] bytes = JasperRunManager.runReportToPdf(reportFile.getPath(), parameters, conn);
response.setContentType("application/pdf");
response.addHeader("Content-Disposition", "attachment; filename=report.pdf");
response.setContentLength(bytes.length);
ServletOutputStream ouputStream = response.getOutputStream();
ouputStream.write(bytes, 0, bytes.length);
ouputStream.flush();
ouputStream.close();
} else if ("excel".equals(type)){
JRXlsExporter exporter = new JRXlsExporter();
ByteArrayOutputStream oStream = new ByteArrayOutputStream();
JasperPrint jasperPrint = JasperFillManager.fillReport(reportFile.getPath(), parameters, conn);
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, oStream);
exporter.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE);
exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE);
exporter.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE);
exporter.exportReport();
byte[] bytes = oStream.toByteArray();
response.setContentType("application/vnd.ms-excel");
response.addHeader("Content-Disposition", "attachment; filename=report.xls");
response.setContentLength(bytes.length);
ServletOutputStream ouputStream = response.getOutputStream();
ouputStream.write(bytes, 0, bytes.length);
ouputStream.flush();
ouputStream.close();
}
}
catch (JRException jre)
{
System.out.println("JRException:" + jre.getMessage());
}
catch (Exception e)
{
System.out.println("Exception:" + e.getMessage());
}
finally{
try
{
conn.close();
}
catch (SQLException ex)
{
// TODO Auto-generated catch block
ex.printStackTrace();
}
}
生成html的参考
//生成html
JRHtmlExporter exporter = new JRHtmlExporter();
ByteArrayOutputStream oStream = new ByteArrayOutputStream();
JasperPrint jasperPrint = JasperFillManager.fillReport(rpt.getPath(), map, con);
exporter.setParameter(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN, Boolean.FALSE);
exporter.setParameter(JRHtmlExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRHtmlExporterParameter.CHARACTER_ENCODING, "utf-8");
exporter.setParameter(JRHtmlExporterParameter.OUTPUT_STREAM, oStream);
exporter.exportReport();
byte[] bytes = oStream.toByteArray();
response.setContentType("text/html");
response.setContentLength(bytes.length);
response.setCharacterEncoding("utf-8");
ServletOutputStream ouputStream = response.getOutputStream();
ouputStream.write(bytes, 0, bytes.length);
ouputStream.flush();
ouputStream.close();
con.close();
out.clear();
out = pageContext.pushBody();
本文介绍了如何利用jasperreports库来生成包括PDF、Excel和HTML在内的各种格式报表,提供了关键代码示例,特别是针对HTML格式的生成。
1万+

被折叠的 条评论
为什么被折叠?



