DynamicReports - DataSource只填充一列?

时间:2016-02-15 09:04:48

标签: java dynamic-reports

我有一个需要DynamicReport的小项目。我得到了报告以获得n列数和n行数但问题是,输出仅使用来自DataSource的所有数据填充1列。我该如何解决这个问题?

public void raports(){

        //get table Data
        DefaultTableModel model = (DefaultTableModel) jTable1.getModel();
        int mColumns = model.getColumnCount();
        int mRows = model.getRowCount();
        String[] arrColumns = new String[mColumns];

        //store the column names into the array
        for(int i=0; i<mColumns; i++){
            arrColumns[i] = model.getColumnName(i);
        }

        JasperReportBuilder report = DynamicReports.report();//a new report
        TextColumnBuilder[] tcb = new TextColumnBuilder[mColumns];

        //create columns
        for (int i=0; i< mColumns; i++) {
            if(i==0){
                tcb[i] = Columns.column(arrColumns[i]+" hello"+i ,arrColumns[i],DataTypes.stringType()).setHorizontalAlignment(HorizontalAlignment.LEFT);
            }else if(i+1==mColumns){
                tcb[i] = Columns.column(arrColumns[i]+" hello"+i ,arrColumns[i],DataTypes.stringType()).setHorizontalAlignment(HorizontalAlignment.LEFT);
            }else{
                tcb[i] = Columns.column(arrColumns[i]+" hello"+i ,arrColumns[i],DataTypes.stringType());
            }

        }

        //create an empty datasource
        DRDataSource dataSource = new DRDataSource(arrColumns);

        //fill the datasource with information
        for (int i = 0; i < mRows; i++) {
            for (int j = 0; j<mColumns; j++){
                dataSource.add(model.getValueAt(i, j).toString());
            }
        }

        StyleBuilder boldStyle = stl.style().bold();  
        StyleBuilder titleStyle = stl.style(boldCenteredStyle)
                .setVerticalAlignment(VerticalAlignment.MIDDLE)
                .setFontSize(15);
        StyleBuilder boldCenteredStyle = stl.style(boldStyle)
                .setHorizontalAlignment(HorizontalAlignment.CENTER);
        StyleBuilder columnTitleStyle  = stl.style(boldCenteredStyle)
                .setBorder(stl.pen1Point())
                .setBackgroundColor(Color.LIGHT_GRAY);

        report
                .setColumnTitleStyle(columnTitleStyle)
                .highlightDetailEvenRows() 
                .columns(tcb)
                .setDataSource(dataSource)
                .title(
                    cmp.horizontalList()
                    .add(
                    cmp.image("C:\\Users\\Innocentus\\Documents\\NetBeansProjects\\JavaApplication9\\src\\seal.png").setFixedDimension(80, 80),
                    cmp.text("School Health \n Services").setStyle(titleStyle).setHorizontalAlignment(HorizontalAlignment.LEFT),
                    cmp.text("Patient List").setStyle(titleStyle).setHorizontalAlignment(HorizontalAlignment.RIGHT))
                    .newRow()
                    .add(cmp.filler().setStyle(stl.style().setTopBorder(stl.pen2Point())).setFixedHeight(10))
                )
                .pageFooter(cmp.pageXofY().setStyle(boldCenteredStyle))
                .pageFooter(Components.pageXofY());
        try {
            report.show();//show the report
            report.toPdf(new FileOutputStream("C:\\Users\\Innocentus\\Documents\\NetBeansProjects\\JavaApplication9\\src\\report.pdf"));//export the report to a pdf file
        } catch (DRException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

的JTable:

+-----------------------------------+
|Title 1 |Title 2 |Title 3 |Title 4 |
+-----------------------------------+
|a       |b       |c       |d       |
|e       |f       |g       |h       |
|i       |j       |k       |l       |
|m       |o       |o       |p       |
+-----------------------------------+

Here is the Picture

1 个答案:

答案 0 :(得分:0)

将数据添加到数据源的for循环不正确,因为它显然是将所有值添加到单个列中。使用以下内容放置for循环:

String[] arr = new String[mColumns];

for (int i = 0; i < mRows; i++) {
                for (int j = 0; j<mColumns; j++){
                    arr[j] = model.getValueAt(i, j).toString();

                }
                dataSource.add(arr);
            }

在上面的代码中,我们将一行的所有列的值添加到字符串数组中,我们将列值列表添加到该行的数据源。希望这有帮助。