使用 DataCollection 的 JasperReport

JasperReport using DataCollection

我正在尝试使用 JRBeanCollectionDataSource 创建报告,但我遇到了问题。我需要在 JTable 中使用复选框打印两条记录,但 JasperReport 中只显示一条记录。

这是我的代码:

for (int i = 0; i < model.getRowCount(); i++) {
    for(int j=0;j< model.getColumnCount();j++){
        Boolean value = (Boolean) model.getValueAt(i, 11);// check state
        if (value) {
            System.out.println(model.getValueAt(i, j));// second column value
            struk=new ArrayList<ListTrxPrint>();
            ListTrxPrint trx=new ListTrxPrint();
            trx.setId(model.getValueAt(i, 0).toString());
            trx.setMsisdn(model.getValueAt(i, 4).toString());
            trx.setExecute_date(model.getValueAt(i, 3).toString());
            trx.setNominal(model.getValueAt(i, 6).toString());
            trx.setSales_price(model.getValueAt(i,7).toString());
            trx.setUser_name(model.getValueAt(i, 1).toString());
            struk.add(trx);
            bean=new JRBeanCollectionDataSource(struk);                       
        }
    }      
}

try{
    JasperDesign design=JRXmlLoader.load("C:\Users\PPS SAMSUL\Documents\RPTStruk.jrxml");
    JasperReport jReport=JasperCompileManager.compileReport(design);               
    JasperPrint jPrint=JasperFillManager.fillReport(jReport, null,bean);
    JasperViewer.viewReport(jPrint, false);
    JasperViewer.getWindows();              
}
catch(Exception e){
    e.printStackTrace();
} 

我的代码有什么问题?

您需要将 ArrayListJRBeanCollectionDataSource 移出您的循环,现在您在每次循环时 重新创建它们 ,而且我不能查看是否需要在所有列上循环。

代码可能类似于:.

struk=new ArrayList<ListTrxPrint>();
for (int i = 0; i < model.getRowCount(); i++) {
   Boolean value = (Boolean) model.getValueAt(i, 11);// check state
   if (value) {
       ListTrxPrint trx=new ListTrxPrint();
       trx.setId(model.getValueAt(i, 0).toString());
       trx.setMsisdn(model.getValueAt(i, 4).toString());
       trx.setExecute_date(model.getValueAt(i, 3).toString());
       trx.setNominal(model.getValueAt(i, 6).toString());
       trx.setSales_price(model.getValueAt(i,7).toString());
       trx.setUser_name(model.getValueAt(i, 1).toString());
       struk.add(trx);
   }
 }      
 bean=new JRBeanCollectionDataSource(struk);