ireport 使用JRMapCollectionDataSource作数据源

1.打印报表类

public class XyErCiReportAction extends BaseReportAction {

	private static final String REPORT_DIR="reports/xy/erci/";
	private static final String REPORT_NAME_XY_ZCXS = REPORT_DIR + "xy-erci";
	private static final String REPORT_NAME_XY_ZCXS_SUB = REPORT_DIR + "xy-erci-sub";
	private static final String REPORT_NAME_XY_ZCXS_SUB_SUB = REPORT_DIR + "xy-erci-sub-sub";
	private static final String REPORT_NAME_XY_ZCXS_SUB_SUB_SUB = REPORT_DIR + "xy-erci-sub-sub-sub";
	
	private String format;
	private CheckTaskService checkTaskService;
	private CheckSpaceService checkSpaceService;
	private CheckItemService checkItemService;
	private CheckAbnormalService checkAbnormalService;
	private CheckOperationService checkOperationService;

	private String taskId;
	
	public String report(){
		
		//主报表数据
		Collection> taskFields = new ArrayList>();
		Map taskField = new HashMap();
		CheckOperation obj = this.checkOperationService.get(taskId);
		String category = "";
		taskField.put("title1","aaa");
		
		//存放所有间隔对应的内容
		Collection> datas = new ArrayList>();
		taskField.put("billNO",obj.getBillNO());
		taskField.put("stName",obj.getStName());
		
		SimpleDateFormat sdf = new SimpleDateFormat();
		String startDateStr = null;
		String endDateStr = null;
		if(obj.getStartDate()!=null){
			startDateStr = sdf.format(obj.getStartDate());
		}
		if(obj.getEndDate()!=null){
			endDateStr = sdf.format(obj.getEndDate());
		}
		taskField.put("startDate",startDateStr);
		taskField.put("endDate",endDateStr);
		taskField.put("principal",obj.getPrincipal());
		taskField.put("auditorName",obj.getAuditorName());
		String checkTaskId=obj.getCheckTaskId();
		String stId = obj.getStId();
		
		List csList = checkSpaceService.findByTaskIdOrderByPlaceOrderno(checkTaskId);
		int size = csList.size();
		for (CheckSpace checkSpace : csList) {
			Map data = new HashMap();
			data.put("devTypeName",checkSpace.getSpaceName());
			
			List list = checkItemService.findDistinctSpaceList2(checkTaskId,checkSpace.getOid());
			Collection> devUnits = new ArrayList>(); 
			for (String pm : list) {
				Collection> devUnitsMap = new ArrayList>();
				Map devUnit = new HashMap();
				devUnit.put("devUnitName", pm);
				
				List items = this.checkItemService.findItemByTaskAndSpaceIdAndTypeName(checkTaskId,checkSpace.getOid(),pm);
				Collection> itemsMap = new ArrayList>(); 
				for (CheckItem item : items) {
					Map itemMap = new HashMap();
					itemMap.put("itemName", item.getCheckItem());
					itemMap.put("checkValue", item.getCheckValue().equals("normal")?"√":"×");
					itemsMap.add(itemMap);
				}
				devUnit.put("itemNames",new JRMapCollectionDataSource(itemsMap));
				devUnits.add(devUnit);
				
			}
			data.put("devUnitList",new JRMapCollectionDataSource(devUnits));
			datas.add(data);
		}
		
		//将数据源作为参数传进子报表
		Map parameters = new HashMap();
		//将数据源作为参数传进子报表
		parameters.put("subDetails", new JRMapCollectionDataSource(datas)); 
		parameters.put("SUBREPORT_DIR",ChkoptConstants.ECXS_SUBREPORT_DIR); 
		
		taskFields.add(taskField);
		JasperUtils.compileReportToJasper(genReportRealPathName(REPORT_NAME_XY_ZCXS));
		JasperUtils.compileReportToJasper(genReportRealPathName(REPORT_NAME_XY_ZCXS_SUB));
		JasperUtils.compileReportToJasper(genReportRealPathName(REPORT_NAME_XY_ZCXS_SUB_SUB));
		JasperUtils.compileReportToJasper(genReportRealPathName(REPORT_NAME_XY_ZCXS_SUB_SUB_SUB));
		  
		List list = new ArrayList();
		list.add(this.genLargeReport(REPORT_NAME_XY_ZCXS, parameters,new JRMapCollectionDataSource(taskFields)));
		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;
	}


	public String getTaskId() {
		return taskId;
	}

	public void setTaskId(String taskId) {
		this.taskId = taskId;
	}

	public void setCheckTaskService(CheckTaskService checkTaskService) {
		this.checkTaskService = checkTaskService;
	}

	public void setCheckSpaceService(CheckSpaceService checkSpaceService) {
		this.checkSpaceService = checkSpaceService;
	}

	public void setCheckItemService(CheckItemService checkItemService) {
		this.checkItemService = checkItemService;
	}
	
	public void setCheckAbnormalService(CheckAbnormalService checkAbnormalService) {
		this.checkAbnormalService = checkAbnormalService;
	}
	public void setCheckOperationService(CheckOperationService checkOperationService) {
		this.checkOperationService = checkOperationService;
	}

}


 

你可能感兴趣的:(ireport)