HSSF导出excel

/**
* 一个导出excel的方法,其实很简单!看这个就基本全会了	
*/
//privHSSF只支持2003版本以前的excel,如果是之后版本的会说版本不匹配,即必须是xls后缀,一开始自己建了一个xlsx后缀的报错,然后自己把后缀改了也不行,必须建文件的时候就要是2003以前的!ate static final String WINNERS_EXCEL_NAME = "WinnersInfo.xls";
public static Map<String, Object> exportWinnersInfo(DispatchContext ctx,Map<String, ? extends Object> context){
		
		
		Delegator delegator = ctx.getDelegator();
		String contactName = (String) context.get("contactName");//联系人姓名
		String contactMobile = (String) context.get("contactMobile");//联系人电话
		String winstartTime = (String) context.get("winstartTime");//中奖时间
		String winEndTime = (String) context.get("winendTime");
		String receiveStartTime = (String) context.get("receiveStartTime");//兑奖时间
		String receiveEndTime = (String) context.get("receiveEndTime");
		String sort = (String) context.get("sort");
		String order = (String) context.get("order");
        
	//取得excel文件
        String templteFilePath = PropertiesUtils.getCfgValue("XXX.excel.path") + File.separator
                + WINNERS_EXCEL_NAME;
        
        File templte = new File(templteFilePath);
        POIFSFileSystem t;//用于解析该excel文件
        
        //条件
  		List<EntityCondition> conds = FastList.newInstance();
  		conds.add(EntityCondition.makeCondition("prizeId",EntityOperator.NOT_EQUAL,""));
  		//按联系人姓名筛选
		if (UtilValidate.isNotEmpty(contactName)) {
			conds.add(EntityCondition.makeCondition("contactName",EntityOperator.LIKE,"%"+contactName+"%"));
		}
		//按联系人电话筛选
		if (UtilValidate.isNotEmpty(contactMobile)) {
			conds.add(EntityCondition.makeCondition("contactMobile",EntityOperator.LIKE,"%"+contactMobile+"%"));
		}
		
		//升降序
		List<String> sortList = null;
		if (UtilValidate.isNotEmpty(sort)&&UtilValidate.isNotEmpty(order)) {
			sortList = FastList.newInstance();
			if ("ASC".equals(order)||"asc".equals(order)) {
				sortList.add("+"+sort);
			}else{
				sortList.add("-"+sort);
			}
		}
  		List<GenericValue> winnerList = FastList.newInstance();
  		Map<String, Object> result = FastMap.newInstance();
		try {
			//中奖时间筛选
			if(UtilValidate.isNotEmpty(winstartTime)&&UtilValidate.isNotEmpty(winEndTime)){
					conds.add(EntityCondition.makeCondition("winningTime",EntityOperator.GREATER_THAN_EQUAL_TO,DateTimeUtil.convertTimestampObj(winstartTime)));
					conds.add(EntityCondition.makeCondition("winningTime",EntityOperator.LESS_THAN_EQUAL_TO,DateTimeUtil.convertTimestampObj(winEndTime)));
			}
			//兑奖时间筛选
			if(UtilValidate.isNotEmpty(receiveStartTime)&&UtilValidate.isNotEmpty(receiveEndTime)){
					conds.add(EntityCondition.makeCondition("receiveTime",EntityOperator.GREATER_THAN_EQUAL_TO,DateTimeUtil.convertTimestampObj(receiveStartTime)));
					conds.add(EntityCondition.makeCondition("receiveTime",EntityOperator.LESS_THAN_EQUAL_TO,DateTimeUtil.convertTimestampObj(receiveEndTime)));
			}
			t = new POIFSFileSystem(new FileInputStream(templte));
			HSSFWorkbook wb = new HSSFWorkbook(t);//获得一个excel工作簿
			HSSFSheet sheet = wb.getSheet("中奖用户信息");//获得一个sheet,用get的话自己现在excel的文件中建好这个sheet可以得到,否则会抛null,也可以用createSheet新建一个也行
			winnerList = delegator.findList("WinnerList", EntityCondition.makeCondition(conds,EntityOperator.AND), null, null, null, false);
			int index = 0;
			HSSFRow row;//一行
			for(index = 0;index<winnerList.size();index++){
				//设置每一列的值
				row = sheet.createRow(index+1);
				row.createCell(0).setCellValue(winnerList.get(index).getString("contactName"));
				row.createCell(1).setCellValue(winnerList.get(index).getString("contactMobile"));
				row.createCell(2).setCellValue(winnerList.get(index).getString("winningTime").substring(0, winnerList.get(index).getString("winningTime").length()-2));//由于数据库中是时间戳类型,所以把.0截断
				row.createCell(3).setCellValue(winnerList.get(index).getString("prizeName"));
				row.createCell(4).setCellValue(winnerList.get(index).getString("nickName"));
				row.createCell(5).setCellValue(winnerList.get(index).getString("receiveTime") == null ? null : winnerList.get(index).getString("receiveTime").substring(0, winnerList.get(index).getString("receiveTime").length()-2));
				row.createCell(6).setCellValue("1".equals(winnerList.get(index).getString("isReceive"))?"已兑换":"未兑换");
			}
			result = RetUtils.retTrue(wb);
			result.put("fileName", "中奖信息明细-" + UtilDateTime.nowDateString("yyyy-MM-dd HH:mm:ss"));
			return result;
			
		} catch (IOException e) {
			Debug.logError(e.getMessage(), MODULE);
			return RetUtils.retFalse(PropertiesUtils.getErrMessage("ERROR_EXPORTS_WINNER_FAILED"));
		} catch (GeneralException e) {
			Debug.logError(e.getMessage(), MODULE);
			return RetUtils.retFalse(PropertiesUtils.getErrMessage("ERROR_EXPORTS_WINNER_FAILED"));
		}
		
	}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值