2010-10-18 64 views
0

我的.jrxml文件包含2文本和一個PARAMjava的碧玉PARAMS靜態

alt text

以下爲碧玉報告我的Java代碼,當我運行的代碼,它創建的PDF文件,但它始終是空的任何幫幫我 ?

public class TestReport { 

    public static void runReport(String reportFile) { 
      try{ 

      Map parameters = new HashMap(); 
      parameters.put("my_var", "Address Report"); 

      JasperDesign jasperDesign = JRXmlLoader.load(reportFile); 
      JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign); 
      JasperPrint jsp = JasperFillManager.fillReport(jasperReport, parameters); 
      JasperExportManager.exportReportToPdfFile(jsp, "/home/ireports/test1.pdf"); 

      }catch(Exception ex) { 
      String connectMsg = "Could not create the report " + ex.getMessage() + " " + ex.getLocalizedMessage(); 
      System.out.println(connectMsg); 
      } 
     } 

    public static void main(String args[]){ 
     runReport("/home/ireports/report1.jrxml"); 
    } 
} 

回答

0

試試這個片段。它會工作,這裏fileName將是.jasper文件編譯版本.jrxml

 try { 
      JasperPrint print = JasperFillManager.fillReport(
        fileName, 
        hm, 
        new JREmptyDataSource()); 
      JRExporter exporter = 
      new net.sf.jasperreports.engine.export.JRPdfExporter(); 
      exporter.setParameter(
      JRExporterParameter.OUTPUT_FILE_NAME, 
      outFileName); 
      exporter.setParameter(
      JRExporterParameter.JASPER_PRINT, print); 
      JasperExportManager.exportReportToPdfFile(print, outFileName); 
      print = null; 
      exporter = null; 
      //JasperViewer.viewReport(print,false); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     }