1

尝试了下面的代码,但没有生成 PDF 报告。刚开始使用 Jasper integartion。请帮忙。

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

1 回答 1

6

试试下面的代码,

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

    }}
于 2013-02-20T13:37:37.563 回答