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

阅读更多
由于在一个里面不能贴太多的代码,故放置代码在此处。
上接:http://jimmy-shine.iteye.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.   
  146. }  

你可能感兴趣的:(Java,.net,Excel,Servlet,SQL)