实现JRDatasource 接口

来源:互联网 发布:马尔可夫链 知乎 编辑:程序博客网 时间:2024/05/22 12:58
  1. 自定义实现JRDatasurce接口
public class TestDataSource implements JRDataSource {private List<Object> data ; private int index = -1; public TestDataSource(List list) {this.data = list; }@Overridepublic Object getFieldValue(JRField field) throws JRException {String fieldName = field.getName(); Object obj = data .get( index ); CheckSpace cp = (CheckSpace)obj;if(fieldName.equals("bill")){return cp.getSpaceName(); }return cp.getSpaceId(); }@Overridepublic boolean next() throws JRException {index ++; return ( index < data .size()); }public List<Object> getData() {return data;}public void setData(List<Object> data) {this.data = data;}public int getIndex() {return index;}public void setIndex(int index) {this.index = index;}}

 

2.打印报表数据类

public class TestReportAction extends BaseReportAction {private static final String REPORT_DIR="reports/test/";private static final String REPORT_NAME_TEST = REPORT_DIR + "test";private String format;public String report(){Map<String, Object> parameters = new HashMap<String, Object>();JasperUtils.compileReportToJasper(genReportRealPathName(REPORT_NAME_TEST));List<JasperPrint> list = new ArrayList<JasperPrint>();List<Object> data = new ArrayList<Object>(); for ( int i = 0; i < 100; i++) { data.add(" 货号 " + i); } list.add(this.genLargeReport(REPORT_NAME_TEST, parameters,new TestDataSource(data)));this.showLarge(list);return null;}/** * 打印报表 *   * @param list  */@SuppressWarnings("unchecked")private void showLarge(List list) {if (ReportsService.REPORT_TYPE_WORD.equals(format))showLargeRTF(list);else if (ReportsService.REPORT_TYPE_EXCEL.equals(format))showLargeXLS(list);elseshowLargePDF(list);}public String getFormat() {return format;}public void setFormat(String format) {this.format = format;}}


 3.新增test.jrxml

0 0
原创粉丝点击