iReport输出pdf,html,word,excel
2013-02-07 16:33
761 查看
转载自:http://zxs19861202.iteye.com/blog/1171118
开发报表使用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();
}
方法二、通过servletOutputStream直接输出
JasperPrint jasperPrint = JasperFillManager.fillReport(reportFile.getPath(),parameters,conn);
JRHtmlExporter exporter = new JRHtmlExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT,jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_WRITER,out);
exporter.setParameter(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN, Boolean.FALSE);
exporter.exportReport();
out.flush();
out.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
a4b4
();
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();
}
开发报表使用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();
}
方法二、通过servletOutputStream直接输出
JasperPrint jasperPrint = JasperFillManager.fillReport(reportFile.getPath(),parameters,conn);
JRHtmlExporter exporter = new JRHtmlExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT,jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_WRITER,out);
exporter.setParameter(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN, Boolean.FALSE);
exporter.exportReport();
out.flush();
out.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
a4b4
();
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();
}
相关文章推荐
- ireport导出各种格式(pdf,excel,word,html,print)
- Ireport 报表导出 Poi + ireport 导出pdf, word ,excel ,html 格式
- ireport导出各种格式(pdf,excel,word,html,print)
- jasperreport + ireport 导出各种类型文件(word,excel,html,pdf,打印) .
- 类似百度文库在线预览文档flash版(支持word、excel、ppt、pdf)+在线预览文档html版
- DataTable导出为word,excel,html,csv,pdf,.txt
- 输出word EXCEL xml html处理---H_print.php
- datatable导出到Word / Excel / PDF / HTML .NET
- android用于打开各种文件的intent,包括以下文件PDF,PPT,WORD,EXCEL,CHM,HTML,TEXT,AUDIO,VIDE(代码待完善)
- 将word、excel、ppt、html、txt,pdf转换成图片源代码
- android打开pdf ppt word excel chm html text 文件的intent
- JAVA读取WORD,EXCEL,PDF,TXT,RTF,HTML文件文本内容的方法示例
- mvc 在view视图中直接输出rdlc报表文件(Excel、PDF、Word)
- Word/Excel/PDF文件转换成HTML整理
- excel,word,txt,img,pdf,转html
- JAVA读取WORD,EXCEL,PDF,TXT,RTF,HTML文件文本内容的方法示例
- 类似百度文库在线预览文档flash版(支持word、excel、ppt、pdf)+在线预览文档html版
- 数据字典生成工具(生成Excel, Word,PDF,html)
- Flex air 调用本地文件,比如Excel,Word,PDF,Notepad,html 文件
- ireport导出excel,html.pdf等格式完整实例