1

我正在使用动态碧玉库为我的报告创建动态列。
这是我的代码:

FastReportBuilder drb = new FastReportBuilder();
          DynamicReport dr = drb.addColumn("State", "state", String.class.getName(),30)
                  .addColumn("Branch", "branch", String.class.getName(),30) // title, property to show, class of the property, width
                  .addColumn("Product Line", "productLine", String.class.getName(),50)
                  .addColumn("Item", "item", String.class.getName(),50)
                  .addColumn("Item Code", "id", Long.class.getName(),20)
                  .addColumn("Quantity", "quantity", Long.class.getName(),30)
                  .addColumn("Amount", "amount", Float.class.getName(),30)
                  .addGroups(2)   // Group by the first two columns
                  .setTitle("November 2006 sales report")
                  .setSubtitle("This report was generateed at" + new Date())
                  .setUseFullPageWidth(true) //make colums to fill the page width
                  .build();      

          JRDataSource ds = new JRBeanCollectionDataSource(TestRepositoryProducts.getDummyCollection());  
          JasperPrint jp = DynamicJasperHelper.generateJasperPrint(dr, new ClassicLayoutManager(), ds);
          JasperViewer.viewReport(jp);

这是我的课TestRepositoryProducts

import java.util.ArrayList;
import java.util.Collection;
import java.util.Vector;

public class TestRepositoryProducts {

    public static Collection<Object> getDummyCollection() {
        Collection<Object> v = new ArrayList<Object>();
        v.add("qsdqsd");
        v.add("qsdqdqs");
        v.add("qsdqdqs");
        v.add("qsdqdqs");
        v.add(32165);
        v.add(65456);
        v.add(1.5);
        return v;
    }

}

这是错误:
在此处输入图像描述

4

1 回答 1

0

JRBeanCollectionDataSource需要一个 Bean 集合,每个代表一行,而不是一个对象集合,每个代表一列(正如您似乎在想的那样)。

Bean 类将需要您定义的每个属性的 getter/setter,例如getState()getBranch()等等。

例如:

public class MyBean {
    private String state;
    // other properties

    public MyBean() {}

    public String getState() { 
        return state;
    }

    public void setState(String s) {
        state = s;
    }

    // other getters and setters
}

您的测试类将创建这些 MyBean 类的集合:

public class TestRepositoryProducts {
    public static Collection<MyBean> getDummyCollection() {
        Collection<MyBean> v = new ArrayList<MyBean>();
        MyBean b1 = new MyBean();
        b1.setState("s1");
        b1.setBranch("b1");
        // set other properties of b1
        v.add(b1);

        // more beans created and added to v
        return v;
    }
于 2015-01-24T09:57:25.803 回答