JasperReport报表设计总结(三)(已完毕)

由于在一个里面不能贴太多的代码,故放置代码在此处。
上接:http://jimmy-shine.javaeye.com/blog/123595

PDF格式的:

java 代码
 
  1. /**  
  2.  * @copyRight Beijing Tsing-Tech Reachway Software Co.,Ltd.  
  3.  * @author Jimmy.Shine 2007-5-12  
  4.  */   
  5. package  cn.com.reachway.framework.report.export;  
  6.   
  7. import  java.io.IOException;  
  8. import  java.io.OutputStream;  
  9. import  java.net.URLEncoder;  
  10. import  java.sql.Connection;  
  11. import  java.util.List;  
  12. import  java.util.Map;  
  13.   
  14. import  javax.servlet.http.HttpServletRequest;  
  15. import  javax.servlet.http.HttpServletResponse;  
  16.   
  17. import  net.sf.jasperreports.engine.JRDataSource;  
  18. import  net.sf.jasperreports.engine.JRException;  
  19. import  net.sf.jasperreports.engine.JRExporterParameter;  
  20. import  net.sf.jasperreports.engine.JasperPrint;  
  21. import  net.sf.jasperreports.engine.export.JRPdfExporter;  
  22. import  net.sf.jasperreports.j2ee.servlets.BaseHttpServlet;  
  23. import  cn.com.reachway.framework.exception.JasperReportException;  
  24. import  cn.com.reachway.framework.report.jasperPrint.JasperPrintWithConnection;  
  25. import  cn.com.reachway.framework.report.jasperPrint.JasperPrintWithDataSource;  
  26.   
  27. /**  
  28.  *   
  29.  */   
  30. public   class  PDFExport {  
  31.   
  32.     /**  
  33.      * 导出报表  
  34.      *   
  35.      * @param request  
  36.      * @param response  
  37.      * @param reportFilePath  
  38.      * @param params  
  39.      * @param con  
  40.      * @param fileName  
  41.      * @throws JasperReportException  
  42.      */   
  43.     public   void  export(HttpServletRequest request, HttpServletResponse response, String reportFilePath, Map params,  
  44.             Connection con, String fileName) throws  JasperReportException {  
  45.   
  46.         JasperPrint jasperPrint = new  JasperPrintWithConnection(reportFilePath, params, con).getJasperPrint();  
  47.         // 将填充完的japserPrint放入session中。   
  48.         request.getSession().setAttribute(BaseHttpServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, jasperPrint);  
  49.         // 拿到japserPrintList   
  50.         List jasperPrintList = BaseHttpServlet.getJasperPrintList(request);  
  51.         // 若没有JasperPrintList,则抛出异常   
  52.         if  (jasperPrintList ==  null ) {  
  53.             throw   new  JasperReportException( "在Http Session中没有找到JasperPrint List" );  
  54.         }  
  55.         try  {  
  56.             OutputStream ouputStream = response.getOutputStream();  
  57.             try  {  
  58.                 response.setContentType("application/pdf" );  
  59.                 response.setCharacterEncoding("UTF-8" );  
  60.                 if  (fileName ==  null  || fileName.equals( "" ))  
  61.                     response.setHeader("Content-Disposition""inline; filename=/"noTitle.pdf/"" );  
  62.                 else   
  63.                     response.setHeader("Content-Disposition""inline; filename=/""   
  64.                             + URLEncoder.encode(fileName, "UTF-8" ) +  ".pdf/"" );  
  65.                 // 使用JRPdfExproter导出器导出pdf   
  66.                 JRPdfExporter exporter = new  JRPdfExporter();  
  67.                 // 设置JasperPrintList   
  68.                 exporter.setParameter(JRExporterParameter.JASPER_PRINT_LIST, jasperPrintList);  
  69.   
  70.                 exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, ouputStream);  
  71.                 exporter.exportReport();  
  72.             } catch  (JRException e) {  
  73.                 e.printStackTrace();  
  74.                 throw   new  JasperReportException( "在导出pdf格式报表时发生错误" );  
  75.             } finally  {  
  76.                 if  (ouputStream !=  null ) {  
  77.                     try  {  
  78.                         ouputStream.close();  
  79.                     } catch  (IOException ex) {  
  80.                     }  
  81.                 }  
  82.             }  
  83.         } catch  (IOException ioe) {  
  84.             ioe.printStackTrace();  
  85.             throw   new  JasperReportException( "从Response中取得OutputStream时发生错误!" );  
  86.         }  
  87.     }  
  88.   
  89.     /**  
  90.      * 导出报表  
  91.      *   
  92.      * @param request  
  93.      * @param response  
  94.      * @param reportFilePath  
  95.      * @param params  
  96.      * @param dataSource  
  97.      * @param fileName  
  98.      * @throws JasperReportException  
  99.      */   
  100.     public   void  export(HttpServletRequest request, HttpServletResponse response, String reportFilePath, Map params,  
  101.             JRDataSource dataSource, String fileName) throws  JasperReportException {  
  102.   
  103.         JasperPrint jasperPrint = new  JasperPrintWithDataSource(reportFilePath, params, dataSource).getJasperPrint();  
  104.         // 将填充完的japserPrint放入session中。   
  105.         request.getSession().setAttribute(BaseHttpServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, jasperPrint);  
  106.         // 拿到japserPrintList   
  107.         List jasperPrintList = BaseHttpServlet.getJasperPrintList(request);  
  108.         // 若没有JasperPrintList,则抛出异常   
  109.         if  (jasperPrintList ==  null ) {  
  110.             throw   new  JasperReportException( "在Http Session中没有找到JasperPrint List" );  
  111.         }  
  112.         try  {  
  113.             OutputStream ouputStream = response.getOutputStream();  
  114.             try  {  
  115.                 response.setContentType("application/pdf" );  
  116.                 response.setCharacterEncoding("UTF-8" );  
  117.                 if  (fileName ==  null  || fileName.equals( "" ))  
  118.                     response.setHeader("Content-Disposition""inline; filename=/"noTitle.pdf/"" );  
  119.                 else   
  120.                     response.setHeader("Content-Disposition""inline; filename=/""   
  121.                             + URLEncoder.encode(fileName, "UTF-8" ) +  ".pdf/"" );  
  122.                 // 使用JRPdfExproter导出器导出pdf   
  123.                 JRPdfExporter exporter = new  JRPdfExporter();  
  124.                 // 设置JasperPrintList   
  125.                 exporter.setParameter(JRExporterParameter.JASPER_PRINT_LIST, jasperPrintList);  
  126.   
  127.                 exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, ouputStream);  
  128.                 exporter.exportReport();  
  129.             } catch  (JRException e) {  
  130.                 e.printStackTrace();  
  131.                 throw   new  JasperReportException( "在导出pdf格式报表时发生错误" );  
  132.             } finally  {  
  133.                 if  (ouputStream !=  null ) {  
  134.                     try  {  
  135.                         ouputStream.close();  
  136.                     } catch  (IOException ex) {  
  137.                     }  
  138.                 }  
  139.             }  
  140.         } catch  (IOException ioe) {  
  141.             ioe.printStackTrace();  
  142.             throw   new  JasperReportException( "从Response中取得OutputStream时发生错误!" );  
  143.         }  
  144.     }  
  145.   

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值