简体   繁体   English

如何将JasperViewer与基于Swing的应用程序集成

[英]How to integrate JasperViewer with Swing based application

can I integrate the JasperReports Viewer to my Swing application as like if I click on view report button from my application then viewer should be opened. 我可以将JasperReports Viewer集成到我的Swing应用程序中,就像我从应用程序中单击“视图报告”按钮,然后应打开查看器一样。 If so could you advise me with code snippet for this integration and in this viewer the save as type should be restricted for PDF only rather than every other download option filter available. 如果可以的话,您可以为我提供有关此集成的代码段的建议,并且在此查看器中,另存为类型应仅限于PDF,而不是其他所有可用的下载选项过滤器。

Please advice me in this regard. 在这方面请给我建议。

Yes you can, Jasper Reports come with a usable (albeit simple) report viewer: 是的,您可以,Jasper Reports带有一个可用的(尽管很简单)报告查看器:

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

The last line is what displays the viewer. 最后一行是显示查看器的内容。 I'm not sure if you can restrict the available output formats to PDF, though. 不过,我不确定是否可以将可用的输出格式限制为PDF。

Let's suppose that report contains the name of your report file (*.jrxml). 假设报告包含报告文件的名称(* .jrxml)。 This program will fill the report with data from the mysql database. 该程序将使用mysql数据库中的数据填充报告。 I hope this solves your problem. 我希望这能解决您的问题。

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