簡體   English   中英

如何將JasperViewer與基於Swing的應用程序集成

[英]How to integrate JasperViewer with Swing based application

我可以將JasperReports Viewer集成到我的Swing應用程序中,就像我從應用程序中單擊“視圖報告”按鈕,然后應打開查看器一樣。 如果可以的話,您可以為我提供有關此集成的代碼段的建議,並且在此查看器中,另存為類型應僅限於PDF,而不是其他所有可用的下載選項過濾器。

在這方面請給我建議。

是的,您可以,Jasper Reports帶有一個可用的(盡管很簡單)報告查看器:

...
           JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
           JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport...);
           JasperViewer.viewReport(jasperPrint);
...

最后一行是顯示查看器的內容。 不過,我不確定是否可以將可用的輸出格式限制為PDF。

假設報告包含報告文件的名稱(* .jrxml)。 該程序將使用mysql數據庫中的數據填充報告。 我希望這能解決您的問題。

public void generate(String report) { // report will contain the name of your file
    try {
        InputStream design = getClass().getResourceAsStream(report);
        JasperDesign jasperdesing = JRXmlLoader.load(design);
        JasperReport jasperReport = JasperCompileManager.compileReport(jasperdesing);
        Connection con = ConnectDB.connect();// connect function in ConnectDB calss is used to connect to the database
        JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, null, con);
        JasperViewer.viewReport(jasperPrint, false);
    } catch (Exception e) {
        System.out.println("Exception " + e);
    }

}
public class ConnectDB {
public static Connection con = null;
public static Connection connect(){
    try{
        Class.forName("com.mysql.jdbc.Driver");
        con = DriverManager.getConnection("jdbc:mysql:///dbname","username","password");
    }catch(Exception e){
        System.out.println();
    }
    return con;
}

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM