JasperReport报表设计总结(二)

 

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.PrintWriter;  
  9. import java.sql.Connection;  
  10. import java.util.Map;  
  11.   
  12. import javax.servlet.http.HttpServletRequest;  
  13. import javax.servlet.http.HttpServletResponse;  
  14.   
  15. import net.sf.jasperreports.engine.JRDataSource;  
  16. import net.sf.jasperreports.engine.JRExporterParameter;  
  17. import net.sf.jasperreports.engine.JasperPrint;  
  18. import net.sf.jasperreports.engine.export.JRHtmlExporter;  
  19. import net.sf.jasperreports.engine.export.JRHtmlExporterParameter;  
  20. import net.sf.jasperreports.j2ee.servlets.ImageServlet;  
  21. import cn.com.reachway.framework.exception.JasperReportException;  
  22. import cn.com.reachway.framework.report.jasperPrint.JasperPrintWithConnection;  
  23. import cn.com.reachway.framework.report.jasperPrint.JasperPrintWithDataSource;  
  24.   
  25. /** 
  26.  * 利用报表生成HTML格式报表 
  27.  */  
  28. public class HTMLExport {  
  29.   
  30.     /** 
  31.      * 导出报表 
  32.      *  
  33.      * @param request 
  34.      * @param response 
  35.      * @param reportFilePath 
  36.      * @param params 
  37.      * @param con 
  38.      * @throws JasperReportException 
  39.      */  
  40.     public void export(HttpServletRequest request, HttpServletResponse response, String reportFilePath, Map params,  
  41.             Connection con) throws JasperReportException {  
  42.         try {  
  43.             PrintWriter out = response.getWriter();  
  44.             try {  
  45.                 response.setContentType("text/html;charset=UTF-8");  
  46.                 JasperPrint jasperPrint = new JasperPrintWithConnection(reportFilePath, params, con).getJasperPrint();  
  47.                 // 使用JRHtmlExproter导出Html格式  
  48.                 JRHtmlExporter exporter = new JRHtmlExporter();  
  49.                 request.getSession().setAttribute(ImageServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, jasperPrint);  
  50.                 exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);  
  51.                 exporter.setParameter(JRExporterParameter.OUTPUT_WRITER, out);  
  52.                 exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI, "./servlets/image?image=");  
  53.                 exporter.setParameter(JRExporterParameter.CHARACTER_ENCODING, "UTF-8");  
  54.                 // 导出  
  55.                 exporter.exportReport();  
  56.             } catch (Exception e) {  
  57.                 e.printStackTrace();  
  58.                 throw new JasperReportException("在导出Html格式报表时发生错误!");  
  59.             } finally {  
  60.                 if (out != null) {  
  61.                     try {  
  62.                         out.close();  
  63.                     } catch (Exception e) {  
  64.                     }  
  65.                 }  
  66.             }  
  67.         } catch (IOException ioe) {  
  68.             ioe.printStackTrace();  
  69.             throw new JasperReportException("从Response中取得PrintWriter时发生错误!");  
  70.         }  
  71.     }  
  72.   
  73.     /** 
  74.      * 导出报表 
  75.      *  
  76.      * @param request 
  77.      * @param response 
  78.      * @param reportFilePath 
  79.      * @param params 
  80.      * @param dataSource 
  81.      * @throws JasperReportException 
  82.      */  
  83.     public void export(HttpServletRequest request, HttpServletResponse response, String reportFilePath, Map params,  
  84.             JRDataSource dataSource) throws JasperReportException {  
  85.         try {  
  86.             PrintWriter out = response.getWriter();  
  87.             try {  
  88.                 response.setContentType("text/html;charset=UTF-8");  
  89.                 JasperPrint jasperPrint = new JasperPrintWithDataSource(reportFilePath, params, dataSource)  
  90.                         .getJasperPrint();  
  91.                 // 使用JRHtmlExproter导出Html格式  
  92.                 JRHtmlExporter exporter = new JRHtmlExporter();  
  93.                 request.getSession().setAttribute(ImageServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, jasperPrint);  
  94.                 exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);  
  95.                 exporter.setParameter(JRExporterParameter.OUTPUT_WRITER, out);  
  96.                 exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI, "./servlets/image?image=");  
  97.                 exporter.setParameter(JRExporterParameter.CHARACTER_ENCODING, "UTF-8");  
  98.                 // 导出  
  99.                 exporter.exportReport();  
  100.             } catch (Exception e) {  
  101.                 e.printStackTrace();  
  102.                 throw new JasperReportException("在导出Html格式报表时发生错误!");  
  103.             } finally {  
  104.                 if (out != null) {  
  105.                     try {  
  106.                         out.close();  
  107.                     } catch (Exception e) {  
  108.                     }  
  109.                 }  
  110.             }  
  111.         } catch (IOException ioe) {  
  112.             ioe.printStackTrace();  
  113.             throw new JasperReportException("从Response中取得PrintWriter时发生错误!");  
  114.         }  
  115.     }  
  116.   
  117. }  

Excel格式的:

java 代码

 

  1. /** 
  2.  * @copyRight Beijing Tsing-Tech Reachway Software Co.,Ltd. 
  3.  * @author Jimmy.Shine 2007-5-13 
  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.JRXlsAbstractExporter;  
  22. import net.sf.jasperreports.engine.export.JRXlsAbstractExporterParameter;  
  23. import net.sf.jasperreports.j2ee.servlets.BaseHttpServlet;  
  24. import cn.com.reachway.framework.exception.JasperReportException;  
  25. import cn.com.reachway.framework.report.jasperPrint.JasperPrintWithConnection;  
  26. import cn.com.reachway.framework.report.jasperPrint.JasperPrintWithDataSource;  
  27.   
  28. /** 
  29.  * 利用报表生成Xls的基类 
  30.  */  
  31. public abstract class BaseExcelExport {  
  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.         JasperPrint jasperPrint = new JasperPrintWithConnection(reportFilePath, params, con).getJasperPrint();  
  46.         // 将填充完的japserPrint放入session中。  
  47.         request.getSession().setAttribute(BaseHttpServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, jasperPrint);  
  48.         // 拿到japserPrintList  
  49.         List jasperPrintList = BaseHttpServlet.getJasperPrintList(request);  
  50.         // 若没有JasperPrintList,则抛出异常  
  51.         if (jasperPrintList == null) {  
  52.             throw new JasperReportException("在Http Session中没有找到JasperPrint List");  
  53.         }  
  54.         try {  
  55.             OutputStream ouputStream = response.getOutputStream();  
  56.             try {  
  57.   
  58.                 response.setContentType("application/xls");  
  59.                 response.setCharacterEncoding("UTF-8");  
  60.                 if (fileName == null || fileName.equals(""))  
  61.                     response.setHeader("Content-Disposition", "inline; filename=/"noTitle.xls/"");  
  62.                 else {  
  63.                     response.setHeader("Content-Disposition", "inline; filename=/""  
  64.                             + URLEncoder.encode(fileName, "UTF-8") + ".xls/"");  
  65.   
  66.                 }  
  67.                 // Xls格式的导出器 JRXlsAbstractExport  
  68.                 JRXlsAbstractExporter exporter = getXlsExporter();  
  69.   
  70.                 // 在导出器中放入要导出的japserPrintList  
  71.                 exporter.setParameter(JRExporterParameter.JASPER_PRINT_LIST, jasperPrintList);  
  72.   
  73.                 exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, ouputStream);  
  74.                 // 设置Xls的属性  
  75.                 exporter.setParameter(JRXlsAbstractExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE);  
  76.                 exporter.setParameter(JRXlsAbstractExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE);  
  77.                 // 导出  
  78.                 exporter.exportReport();  
  79.             } catch (JRException e) {  
  80.                 e.printStackTrace();  
  81.                 throw new JasperReportException("在生成XLS报表时发生错误!");  
  82.             }  
  83.   
  84.             finally {  
  85.                 if (ouputStream != null) {  
  86.                     try {  
  87.                         ouputStream.close();  
  88.                     } catch (IOException ex) {  
  89.                     }  
  90.                 }  
  91.             }  
  92.         } catch (IOException ioe) {  
  93.             ioe.printStackTrace();  
  94.             throw new JasperReportException("从Response中取得OutputStream时发生错误!");  
  95.         }  
  96.   
  97.     }  
  98.   
  99.     /** 
  100.      * 导出报表 
  101.      *  
  102.      * @param request 
  103.      * @param response 
  104.      * @param reportFilePath 
  105.      * @param params 
  106.      * @param dataSource 
  107.      * @param fileName 
  108.      * @throws JasperReportException 
  109.      */  
  110.     public void export(HttpServletRequest request, HttpServletResponse response, String reportFilePath, Map params,  
  111.             JRDataSource dataSource, String fileName) throws JasperReportException {  
  112.         JasperPrint jasperPrint = new JasperPrintWithDataSource(reportFilePath, params, dataSource).getJasperPrint();  
  113.         // 将填充完的japserPrint放入session中。  
  114.         request.getSession().setAttribute(BaseHttpServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, jasperPrint);  
  115.         // 拿到japserPrintList  
  116.         List jasperPrintList = BaseHttpServlet.getJasperPrintList(request);  
  117.         // 若没有JasperPrintList,则抛出异常  
  118.         if (jasperPrintList == null) {  
  119.             throw new JasperReportException("在Http Session中没有找到JasperPrint List");  
  120.         }  
  121.         try {  
  122.             OutputStream ouputStream = response.getOutputStream();  
  123.             try {  
  124.   
  125.                 response.setContentType("application/xls");  
  126.                 response.setCharacterEncoding("UTF-8");  
  127.                 if (fileName == null || fileName.equals(""))  
  128.                     response.setHeader("Content-Disposition", "inline; filename=/"noTitle.xls/"");  
  129.                 else {  
  130.                     response.setHeader("Content-Disposition", "inline; filename=/""  
  131.                             + URLEncoder.encode(fileName, "UTF-8") + ".xls/"");  
  132.   
  133.                 }  
  134.                 // Xls格式的导出器 JRXlsAbstractExport  
  135.                 JRXlsAbstractExporter exporter = getXlsExporter();  
  136.   
  137.                 // 在导出器中放入要导出的japserPrintList  
  138.                 exporter.setParameter(JRExporterParameter.JASPER_PRINT_LIST, jasperPrintList);  
  139.   
  140.                 exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, ouputStream);  
  141.                 // 设置Xls的属性  
  142.                 exporter.setParameter(JRXlsAbstractExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE);  
  143.                 exporter.setParameter(JRXlsAbstractExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE);  
  144.                 // 导出  
  145.                 exporter.exportReport();  
  146.             } catch (JRException e) {  
  147.                 e.printStackTrace();  
  148.                 throw new JasperReportException("在生成XLS报表时发生错误!");  
  149.             }  
  150.   
  151.             finally {  
  152.                 if (ouputStream != null) {  
  153.                     try {  
  154.                         ouputStream.close();  
  155.                     } catch (IOException ex) {  
  156.                     }  
  157.                 }  
  158.             }  
  159.         } catch (IOException ioe) {  
  160.             ioe.printStackTrace();  
  161.             throw new JasperReportException("从Response中取得OutputStream时发生错误!");  
  162.         }  
  163.   
  164.     }  
  165.   
  166.     protected abstract JRXlsAbstractExporter getXlsExporter();  
  167.   
  168. }  

java 代码

 

  1. /** 
  2.  * @copyRight Beijing Tsing-Tech Reachway Software Co.,Ltd. 
  3.  * @author Jimmy.Shine 2007-5-13 
  4.  */  
  5. package cn.com.reachway.framework.report.export;  
  6.   
  7. import net.sf.jasperreports.engine.export.JRXlsAbstractExporter;  
  8. import net.sf.jasperreports.engine.export.JRXlsExporter;  
  9.   
  10. /** 
  11.  * 利用报表使用POI生成XLS报表 
  12.  */  
  13. public class XlsPOIExport extends BaseExcelExport {  
  14.   
  15.     protected JRXlsAbstractExporter getXlsExporter() {  
  16.         return new JRXlsExporter();  
  17.     }  
  18.   
  19. }  
  20.     

java 代码

 

  1. /** 
  2.  * @copyRight Beijing Tsing-Tech Reachway Software Co.,Ltd. 
  3.  * @author Jimmy.Shine 2007-5-13 
  4.  */  
  5. package cn.com.reachway.framework.report.export;  
  6.   
  7. import net.sf.jasperreports.engine.export.JExcelApiExporter;  
  8. import net.sf.jasperreports.engine.export.JRXlsAbstractExporter;  
  9.   
  10. /** 
  11.  * 利用报表使用JExcel生成xls报表 
  12.  */  
  13. public class XlsJExcelExport extends BaseExcelExport {  
  14.   
  15.     protected JRXlsAbstractExporter getXlsExporter() {  
  16.         return new JExcelApiExporter();  
  17.     }  
  18. }    

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.   

你可能感兴趣的:(html,session,Stream,null,报表,encoding)