(八)Java jaspereport程序导出pdf及excel报表

jaspereport导出报表代码很容易。 

加载jasper文件,接收map参数导出报表至指定目录。 

Java代码  收藏代码
  1.        /** 
  2.  * jasper文件,导出pdf文件 
  3.  *  
  4.  * @param fileName 
  5.  * @param params 
  6.  * @param exportPath 
  7.  */  
  8. public String exportPDFReport(String Folder, String exportPath, String reportName,  
  9.         Map params) {  
  10.     ReportService rs = new ReportService();  
  11.     // 根据报表名字,得到报表jasper文件名字  
  12.     String jasperName = rs.getReportEnName(reportName);  
  13.   
  14.     File file = new File(Folder + "\\" + jasperName + ".jasper");  
  15.   
  16.     exportPath = exportPath + "\\" + System.currentTimeMillis() + ".pdf";  
  17.   
  18.     JasperReport report = null;  
  19.     JasperPrint jasperPrint = null;  
  20.     try {  
  21.         report = (JasperReport) JRLoader.loadObject(file);  
  22.     } catch (JRException e) {  
  23.         e.printStackTrace();  
  24.     }  
  25.       
  26.     //取数据库连接key  
  27.     String key = rs.getConnectionKeyWords(jasperName);  
  28.   
  29.     // 获得jdbc连接  
  30.     Connection conn = new DBUtil().getConnection(key);  
  31.   
  32.     try {  
  33.         jasperPrint = JasperFillManager.fillReport(report, params, conn);  
  34.     } catch (JRException e) {  
  35.         e.printStackTrace();  
  36.     } finally {  
  37.         if (conn != null) {  
  38.             try {  
  39.                 conn.close();  
  40.             } catch (SQLException e) {  
  41.                 e.printStackTrace();  
  42.             }  
  43.         }  
  44.     }  
  45.   
  46.     JRPdfExporter pdfExporter = new JRPdfExporter();  
  47.     pdfExporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);  
  48.     pdfExporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME,  
  49.             exportPath);// exportPath=路径+文件名  
  50.     try {  
  51.         pdfExporter.exportReport();  
  52.   
  53.         System.out.println("export pdf success!");  
  54.     } catch (JRException e) {  
  55.         e.printStackTrace();  
  56.     }  
  57.       
  58.     return exportPath;  
  59. }  
  60.   
  61. /** 
  62.  * jasper文件,导出EXCEL文件 
  63.  * @param Folder 
  64.  * @param reportName 
  65.  * @param params 
  66.  * @return 
  67.  */  
  68. public String exportEXCELReport(String Folder, String exportPath, String reportName,  
  69.         Map params) {  
  70.     ReportService rs = new ReportService();  
  71.     // 根据报表名字,得到报表jasper文件名字  
  72.     String jasperName = rs.getReportEnName(reportName);  
  73.   
  74.     File file = new File(Folder + "\\" + jasperName + ".jasper");  
  75.   
  76.     exportPath = exportPath + "\\" + System.currentTimeMillis() + ".xls";  
  77.   
  78.     JasperReport report = null;  
  79.     JasperPrint jasperPrint = null;  
  80.     try {  
  81.         report = (JasperReport) JRLoader.loadObject(file);  
  82.     } catch (JRException e) {  
  83.         e.printStackTrace();  
  84.     }  
  85.       
  86.     //取数据库连接key  
  87.     String key = rs.getConnectionKeyWords(jasperName);  
  88.   
  89.     // 获得jdbc连接  
  90.     Connection conn = new DBUtil().getConnection(key);  
  91.   
  92.     try {  
  93.         jasperPrint = JasperFillManager.fillReport(report, params, conn);  
  94.     } catch (JRException e) {  
  95.         e.printStackTrace();  
  96.     } finally {  
  97.         if (conn != null) {  
  98.             try {  
  99.                 conn.close();  
  100.             } catch (SQLException e) {  
  101.                 e.printStackTrace();  
  102.             }  
  103.         }  
  104.     }  
  105.   
  106.     JRXlsExporter excelExporter = new JRXlsExporter();  
  107.     excelExporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);  
  108.     excelExporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, exportPath);  
  109.     try {  
  110.         excelExporter.exportReport();  
  111.   
  112.         System.out.println("export excel success!");  
  113.     } catch (JRException e) {  
  114.         e.printStackTrace();  
  115.     }  
  116.       
  117.     return exportPath;  
  118. }  


程序依赖的jar,可能是这几个具体记不清。 

 

posted @ 2015-05-11 16:51  疯子110  阅读(573)  评论(0)    收藏  举报