如何在DefaultTableModel传递的Jasper报表中显示数据

时间:2015-07-21 14:33:48

标签: java jasper-reports

我正在研究Java Swing中的一个项目并在Jasper Report中进行报告。

我使用Jasper Report设计了报告,我正在调用该报告 Java类。 现在我想以TableModel的形式发送数据以在报告表中设置值,但是jasper报告表没有显示以TableModel形式设置的表数据。

即使是报告文件也没有通过JAR打开。如果我正在创建jar,那么Jasper Report页面就会抛出异常。

我想在报表中发送表模型数据,如java代码中的tableModelData。我使用以下代码。

   package pmc;
   import java.util.HashMap;
   import javax.swing.table.DefaultTableModel;
   import net.sf.jasperreports.engine.JRException;
   import net.sf.jasperreports.engine.JasperCompileManager;
   import net.sf.jasperreports.engine.JasperFillManager;
   import net.sf.jasperreports.engine.JasperPrint;
   import net.sf.jasperreports.engine.data.JRTableModelDataSource;
   import net.sf.jasperreports.view.JasperViewer;

   public class SimpleReport1 {

   DefaultTableModel tableModel;

   public SimpleReport1() {
     JasperPrint jasperPrint = null;
      TableModelData();
       try {
            JasperCompileManager.compileReportToFile("src/reports/report1.jrxml");
        jasperPrint = JasperFillManager.fillReport("src/reports/report1.jasper", new HashMap(),
                new JRTableModelDataSource(tableModel));
        JasperViewer jasperViewer = new JasperViewer(jasperPrint);
        jasperViewer.setVisible(true);
    } catch (JRException ex) {
        ex.printStackTrace();

} }

private void TableModelData() {
    String[] columnNames = {"Id", "Name", "Department", "Email"};
    String[][] data = {
        {"111", "G Conger", " Orthopaedic", "jim@wheremail.com"},
        {"222", "A Date", "ENT", "adate@somemail.com"},
        {"333", "R Linz", "Paedriatics", "rlinz@heremail.com"},
        {"444", "V Sethi", "Nephrology", "vsethi@whomail.com"},
        {"555", "K Rao", "Orthopaedics", "krao@whatmail.com"},
        {"666", "V Santana", "Nephrology", "vsan@whenmail.com"},
        {"777", "J Pollock", "Nephrology", "jpol@domail.com"},
        {"888", "H David", "Nephrology", "hdavid@donemail.com"},
        {"999", "P Patel", "Nephrology", "ppatel@gomail.com"},
        {"101", "C Comer", "Nephrology", "ccomer@whymail.com"}
    };
    tableModel = new DefaultTableModel(data, columnNames);
}
public static void main(String[] args) {
    new SimpleReport1();
}
}

1 个答案:

答案 0 :(得分:0)

更改此行:

JasperCompileManager.compileReportToFile("src/reports/report1.jrxml");

为:

JasperCompileManager.compileReportToFile("src/reports/report1.jrxml","src/reports/report1.jasper");