简体   繁体   English

Jasper Report集成到Java

[英]Jasper Report integration to Java

Tried the code below but the PDF reports is not generated. 尝试以下代码,但未生成PDF报告。 Just started with Jasper integartion. 刚开始使用Jasper整合。 Please help. 请帮忙。

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

Try the below code , 试试下面的代码,

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());
    }

    }}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM