实现JRDatasource 接口

  1. 自定义实现JRDatasurce接口
public class TestDataSource implements JRDataSource {
	private List data ; 
	private int index = -1; 


	
	public TestDataSource(List list) {
		this.data = list; 
	}

	@Override
	public 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(); 
	}

	@Override
	public boolean next() throws JRException {
		index ++; 
		return ( index < data .size()); 

	}

	public List getData() {
		return data;
	}

	public void setData(List 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 parameters = new HashMap();
		JasperUtils.compileReportToJasper(genReportRealPathName(REPORT_NAME_TEST));
		List list = new ArrayList();
		List data = new ArrayList(); 
		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);
		else
			showLargePDF(list);
	}

	public String getFormat() {
		return format;
	}

	public void setFormat(String format) {
		this.format = format;
	}

} 
  


 3.新增test.jrxml

实现JRDatasource 接口_第1张图片

你可能感兴趣的:(ireport)