开发报表使用ireport开发还是比较快和方便的,以下整理jsp将jasper文件生成html,word,excel,pdf格式的代码:
ireport的使用教程可以参见一下附件:
1、生成html格式:
//输出html格式
Map<String,String> parameters = new HashMap<String,String>();
parameters.put("title","我们的产品");
Connection con = null;
try {
con=Conmysql.getCon();
//返回生成文件路径
String file=JasperRunManager.runReportToHtmlFile(application.getRealPath ("reports/test.jasper"), parameters, con);
response.sendRedirect("reports/test.html");
}
catch (Exception e) {
System.out.println(e);
}finally{
con.close();
}
2、生成PDF格式文件
//生成pdf格式
Map<String,String> parameters = new HashMap<String,String>();
parameters.put("title","我们的产品");
Connection con = null;
try {
con=Conmysql.getCon();
System.out.println("连接数据库成功");
byte[] bytes = JasperRunManager.runReportToPdf(application.getRealPath("reports/test.jasper"), parameters, con);
response.setContentType("application/pdf");
response.setContentLength(bytes.length);
ServletOutputStream ouputStream = response.getOutputStream();
ouputStream.write(bytes, 0, bytes.length);
ouputStream.flush();
ouputStream.close();
}
catch (Exception e) {
System.out.println(e);
}finally{
con.close();
}
3、生成excel格式的文件
//生成excel格式
Connection con=null;
con=Conmysql.getCon();
Map<String,String> parameters = new HashMap<String,String>();
parameters.put("title","我们的产品");
try{
JasperPrint jasperPrint = JasperFillManager.fillReport(application.getRealPath("reports/test.jasper"),parameters,con);
ByteArrayOutputStream oStream = new ByteArrayOutputStream();
JRXlsExporter exporter = new JRXlsExporter();
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);// 删除多余的ColumnHeader
exporter.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND,Boolean.FALSE);// 显示边框
exporter.exportReport();
byte[] bytes = oStream.toByteArray();
if(bytes != null && bytes.length > 0) {
response.reset();
response.setContentType("application/vnd.ms-excel");
response.setContentLength(bytes.length);
ServletOutputStream ouputStream = response.getOutputStream();
ouputStream.write(bytes,0,bytes.length);
ouputStream.flush();
ouputStream.close();
}else{
out.print("bytes were null!");
}
}catch(JRException ex){
out.print("Jasper Output Error:"+ex.getMessage());
}finally{
con.close();
}
4、生成word格式的文件:
//生成word格式
Map<String,String> parameters = new HashMap<String,String>();
parameters.put("title","我们的产品");
Connection con = null;
try {
con=Conmysql.getCon();
System.out.println("连接数据库成功");
JasperPrint jasperPrint = JasperFillManager.fillReport(application.getRealPath("reports/test.jasper"),parameters,con);
ByteArrayOutputStream oStrEeam = new ByteArrayOutputStream();
JRExporter exporter = new JRRtfExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, oStrEeam);
exporter.exportReport();
byte[] bytes = oStrEeam.toByteArray();
if(bytes != null && bytes.length > 0) {
response.reset();
response.setContentType("application/ms_word");
response.setHeader("Content-disposition", "attachment; filename=zhiyou_erp.doc");
response.setContentLength(bytes.length);
ServletOutputStream ouputStream = response.getOutputStream();
ouputStream.write(bytes, 0, bytes.length);
ouputStream.flush();
ouputStream.close();
}
}
catch (Exception e) {
System.out.println(e);
}finally{
con.close();
}