ireport 使用

  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. }   


你可能感兴趣的:(IREPORT)