我如何在java中加入已编译的jasper文件和我的项目?

时间:2023-01-13 11:13:47

I have design my project in iReport and I have compiled successfully. Now I want to add the compiled jasper file to add in my java project. May any one help me?

我在iReport中设计了我的项目,并且我已成功编译。现在我想添加已编译的jasper文件以添加到我的java项目中。愿任何人帮助我吗?

2 个解决方案

#1


0  

Just add them in some package in your source tree. Your build process or IDE should copy them in your classes directory or in the generated jar file, along with your classes. The reports may then be loaded with Class.getResource() or Class.getResourceAsStream().

只需将它们添加到源树中的某个包中即可。您的构建过程或IDE应将它们与您的类一起复制到类目录或生成的jar文件中。然后可以使用Class.getResource()或Class.getResourceAsStream()加载报告。

#2


0  

Here is the code you need in order to get two values from two combo-boxes, add them to a HashMap and pass the map to iReport. These paramaters, for this example "storeName" and "actionCode", are used to specify values for the query which is stored inside the iReport. The report jasper file is "../ireps/AccessCounter.jrxml". The viewer is a JDialog. If you have no parameters to pass to your report just skip the map.put. con is the connection to the database.

以下是从两个组合框中获取两个值所需的代码,将它们添加到HashMap并将映射传递给iReport。这些参数(例如“storeName”和“actionCode”)用于指定存储在iReport中的查询的值。报告jasper文件是“../ireps/AccessCounter.jrxml”。观众是JDialog。如果您没有要传递给报表的参数,请跳过map.put。 con是与数据库的连接。

Make sure that you include the necessary jar file to your path.

确保在路径中包含必要的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());
} 

#1


0  

Just add them in some package in your source tree. Your build process or IDE should copy them in your classes directory or in the generated jar file, along with your classes. The reports may then be loaded with Class.getResource() or Class.getResourceAsStream().

只需将它们添加到源树中的某个包中即可。您的构建过程或IDE应将它们与您的类一起复制到类目录或生成的jar文件中。然后可以使用Class.getResource()或Class.getResourceAsStream()加载报告。

#2


0  

Here is the code you need in order to get two values from two combo-boxes, add them to a HashMap and pass the map to iReport. These paramaters, for this example "storeName" and "actionCode", are used to specify values for the query which is stored inside the iReport. The report jasper file is "../ireps/AccessCounter.jrxml". The viewer is a JDialog. If you have no parameters to pass to your report just skip the map.put. con is the connection to the database.

以下是从两个组合框中获取两个值所需的代码,将它们添加到HashMap并将映射传递给iReport。这些参数(例如“storeName”和“actionCode”)用于指定存储在iReport中的查询的值。报告jasper文件是“../ireps/AccessCounter.jrxml”。观众是JDialog。如果您没有要传递给报表的参数,请跳过map.put。 con是与数据库的连接。

Make sure that you include the necessary jar file to your path.

确保在路径中包含必要的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());
}