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

我可以将JasperReports Viewer集成到我的Swing应用程序中,就像我从我的应用程序中单击查看报告按钮一样,然后应该打开查看器。 如果是这样,你可以告诉我这个集成的代码片段,在这个查看器中,应该仅限制为PDF而不是每个其他可用的下载选项filter。

请在这方面给我建议。

是的,你可以,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; } 

}