导出数据 org.apache.poi.hssf.usermodel.HSSFWorkbook

		
		Map params = (Map) request.getSession().getAttribute(DOWNLOADPARAMSKEY);
		
		if(null == params){
			response.getWriter().print("error request");
			return;
		}
		
		String fileName = String.format("%s.xls", new Date().getTime());
		response.setContentType("application/octet-stream");
		response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
		
		List applyInfoList = signingOnlineNewService.getExportSigningInfoList(params);
		log.info("data size:"+ applyInfoList.size());
		
		HSSFWorkbook book = new HSSFWorkbook();
		
		HSSFSheet sheet = book.createSheet("在线签约申请记录");
		sheet.setColumnWidth(0, 30 * 256); 
		sheet.setColumnWidth(1, 13 * 256); 
		sheet.setColumnWidth(2, 10 * 256); 
		sheet.setColumnWidth(3, 35 * 256); 
		sheet.setColumnWidth(4, 10 * 256); 
		sheet.setColumnWidth(5, 10 * 256); 
		sheet.setColumnWidth(6, 20 * 256); 
		sheet.setColumnWidth(7, 10 * 256); 
		sheet.setColumnWidth(8, 20 * 256); 
		sheet.setColumnWidth(9, 10 * 256); 
		sheet.setColumnWidth(10, 10 * 256); 
		sheet.setColumnWidth(11, 12 * 256); 
		sheet.setColumnWidth(12, 10 * 256); 
		sheet.setColumnWidth(13, 10 * 256); 
		
		HSSFRow titleRow = sheet.createRow((short)0);

		CellStyle titleStyle = getTitleStyle(book);
		for (int i = 0; i < TITLES.length; i++) {
			HSSFCell cell = titleRow.createCell(i);
			cell.setCellStyle(titleStyle);
			cell.setCellValue(TITLES[i]);
		}
		
//		CellStyle gridStyle = getRowStyle(book);
		for (int i = 0; i < applyInfoList.size(); i++) {
			HSSFRow gridRow = sheet.createRow(1+i);
//			gridRow.setRowStyle(gridStyle);
			 // 合同流水号、
			gridRow.createCell(0).setCellValue(applyInfoList.get(i).getSerialNum());
			 // 申请人手机号、
			gridRow.createCell(1).setCellValue(applyInfoList.get(i).getApplyMobile());
			 // 个人/公司名称、
			gridRow.createCell(2).setCellValue(applyInfoList.get(i).getCustomerName());
			 // 个人/公司地址、
			gridRow.createCell(3).setCellValue(applyInfoList.get(i).getCustomerAddr());
			 // 联系人、
			gridRow.createCell(4).setCellValue(applyInfoList.get(i).getApplyUserName());
			 // 申请状态、
			gridRow.createCell(5).setCellValue(applyInfoList.get(i).getStatus());
			 // 申请时间、
			gridRow.createCell(6).setCellValue(applyInfoList.get(i).getApplyTm());
			 // 生效日期、
			gridRow.createCell(7).setCellValue(applyInfoList.get(i).getTakeEffectTM());
			 // 月结账号、
			gridRow.createCell(8).setCellValue(applyInfoList.get(i).getCardNo());
			 // 开发人员工号、
			gridRow.createCell(9).setCellValue(applyInfoList.get(i).getDevEmpNum());
			 // 网络代码、
			gridRow.createCell(10).setCellValue(applyInfoList.get(i).getNetCode());
			 // 快递决策人手机、
			gridRow.createCell(11).setCellValue(applyInfoList.get(i).getDecMakerMobile());
			 // 快递决策人电话、
			gridRow.createCell(12).setCellValue(applyInfoList.get(i).getDecMakerTelePhone());
			 // 主要寄件人手机
			gridRow.createCell(13).setCellValue(applyInfoList.get(i).getMostConsignorMobile());
		}
		book.write(response.getOutputStream());
		//清理session中的参数
		//这里暂时不清理,因为360浏览器请求了两次当前网关,导致下载失败!(第一次请求时正常,第二次请求时session中的参数已经被清空了,故会报错!)
//		request.getSession().removeAttribute(DOWNLOADPARAMSKEY);
	
 
  
 
  
	/**
	 * 列标题样式
	 * 
	 * @param wb
	 * @return
	 */
	private CellStyle getTitleStyle(HSSFWorkbook wb) {
		CellStyle style = wb.createCellStyle();
		style.setFont(getFont(wb));
		style.setAlignment(CellStyle.ALIGN_CENTER);
		style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
		style.setFillPattern(CellStyle.SOLID_FOREGROUND);
		style.setBorderBottom((short) 1);
		style.setBorderLeft((short) 1);
		style.setBorderRight((short) 1);
		style.setBorderTop((short) 1);
		style.setBottomBorderColor(HSSFColor.BLACK.index);
		return style;
	}

	/**
	 * 字体样式
	 * 
	 * @param wb
	 * @return
	 */
	private HSSFFont getFont(HSSFWorkbook wb) {
		HSSFFont font = wb.createFont();
		font.setFontName("Arial");
		font.setFontHeightInPoints((short) 10);
		return font;
	}


 
  
 
 

你可能感兴趣的:(导出数据 org.apache.poi.hssf.usermodel.HSSFWorkbook)