2013-02-20 68 views
1

嘗試了下面的代碼,但未生成PDF報告。剛開始使用Jasper integartion。請幫忙。將Jasper Report與Java集成

public String mainReport() { 
     HashMap jrxmlParams = null; 


     try { 
     System.out.println("Start ...."); 
     // Get jasper report 
     String jrxmlFileName = "C:/Jasper/DP_crosstabs.jrxml"; 


     JasperReport objJReport = JasperCompileManager.compileReport(jrxmlFileName); 

     connection = getConnection(); 

     jrxmlParams = new HashMap(); 
     jrxmlParams.put("ID", "null"); 

     JasperPrint objJPrint = JasperFillManager.fillReport(objJReport, jrxmlParams, connection); 

     ByteArrayOutputStream objBAOutputStream = new ByteArrayOutputStream(); 

     JasperExportManager.exportReportToPdfStream(objJPrint, objBAOutputStream); 



     System.out.println("Done exporting reports to pdf"); 



     } catch (Exception e) { 
     System.out.print("Exceptiion" + e); 
     } 
    return null; 
    } 
+0

添加了所有相關jar文件,可以幫助的人這 – Sainik 2013-02-20 11:09:26

+0

是一個桌面應用程序? – psabbate 2013-02-20 12:21:41

回答

5

嘗試下面的代碼,

public static void main(String[] args) { 

    String reportPath = "/report3.jasper"; 
    Map<String, Object> params = new HashMap<String, Object>(); 
    Connection connection; 

    try { 


      Class.forName("com.mysql.jdbc.Driver"); 
      connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/person","root","password"); 

     System.out.println("Filling report..."); 
     JasperPrint jasperPrint = JasperFillManager.fillReport(reportPath, params, connection); 
     JasperExportManager.exportReportToPdfFile(jasperPrint, 
          "/Test.pdf"); 
     JasperViewer.viewReport(jasperPrint, false); 
     connection.close(); 
    } catch (Exception e) { 
     System.out.println(e.getMessage()); 
    } 

    }} 
+0

感謝您的幫助Sadagopan,它的工作:)...但根據項目要求,我們必須生成HTML格式的報告。能夠使用導出管理器創建文件,但無法在同一實例中查看相同的文件。請幫助! – Sainik 2013-02-20 15:49:27

+0

Hi Jasper查看器將在所有格式的同一實例中觸發生成的報告。請將上面的標記標記爲答案。謝謝 – Sads 2013-02-20 17:48:06

+0

請投票答覆。謝謝 – Sads 2013-03-06 12:03:52