2011-03-09 85 views

回答

0

只需將它們添加到源代碼樹中的某些包中即可。您的構建過程或IDE應將它們與您的類一起復制到您的類目錄或生成的jar文件中。然後這些報告可以加載Class.getResource()Class.getResourceAsStream()

0

這是您需要的代碼,以便從兩個組合框中獲取兩個值,將它們添加到HashMap並將地圖傳遞給iReport。這些參數用於此示例「storeName」和「actionCode」,用於指定存儲在iReport內部的查詢的值。報告碧玉文件是「../ireps/AccessCounter.jrxml」。查看器是一個JDialog。如果你沒有參數傳遞給你的報告,只需跳過map.put。 con是與數據庫的連接。

確保您將必要的jar文件包含到路徑中。

try { 
    String shopName = jComboBox1.getSelectedItem().toString(); 
    String actionCode = jComboBox2.getSelectedItem().toString(); 
    HashMap<String, Object> map = new HashMap<String, Object>(); 
    map.put("storeName", shopName); 
    map.put("actionCode", actionCode); 

    URL reportFileURL = getClass().getResource("../ireps/AccessCounter.jrxml"); 
    File reportFile = new File(reportFileURL.toURI()); 
    JasperDesign jasperDesign = JRXmlLoader.load(reportFile); 
    JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign); 
    JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, map, con); 
    JasperViewer jv = new JasperViewer(jasperPrint); 
    JDialog viewer = new JDialog(this, "Batch Report", true); 
    viewer.setBounds(jv.getBounds()); 
    viewer.getContentPane().add(jv.getContentPane()); 
    viewer.setResizable(true); 
    viewer.setIconImage(jv.getIconImage()); 
    viewer.setVisible(true); 
} catch (JRException exc) { 
    System.out.println(exc.getMessage()); 
} catch (URISyntaxException exs) { 
    System.out.println(exs.getMessage()); 
} 
相關問題