poi导出excel定义单元格某几列为double类型

poi导出excel定义单元格某几列为double类型

private static Log LOG = LogFactory.getLog(ExcelHelper.class);

private static int MaxSize = 1000000;
private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

public static SXSSFWorkbook exportExcel(String excel_name, List<ExeclConfigDomain> headerList, List dataList) throws Exception {

	SXSSFWorkbook wb = createExecl(headerList, dataList);

	HttpServletResponse response = WebContext.getResponse();
	OutputStream output = null;

	/**
	 * 输出到excel
	 */
	try {
		output = response.getOutputStream();
		response.setContentType("application/x-msdownload:charset=gb2312");
		response.setHeader("Content-Disposition", "attachment;filename=" + new String((excel_name).getBytes("gb2312"), "ISO-8859-1") + ".xlsx");
		wb.write(output);
		output.close();
		wb.close();

	} catch (IOException ie) {
		LOG.error(ie.getMessage());
	} finally {
		if (output != null) {
			try {
				output.close();
			} catch (IOException e) {
				LOG.error(e.getMessage());
			}
		}
	}
	response.setContentType("application/octet-stream;charset=UTF-8");
	response.setHeader("Content-Disposition", "attachment;filename=" + "aaa");
	response.addHeader("Pargam", "no-cache");
	response.addHeader("Cache-Control", "no-cache");
	OutputStream os = response.getOutputStream();
	wb.write(os);
	os.flush();
	os.close();
	return wb;
}

/****
* 创建excel工作簿
* @param headerList
* @param dataList
* @return
* @throws Exception
*/

public static SXSSFWorkbook createExecl(List<ExeclConfigDomain> headerList,List dataList) throws Exception {

	short rownum = 0;
	SXSSFWorkbook wb = new SXSSFWorkbook(3000);

	Sheet sheet = wb.createSheet("sheet1");
	Row row0 = sheet.createRow(rownum);
	// 设置execl 头
	if (headerList != null && headerList.size() > 0) {
		for (int i = 0; i < headerList.size(); i++) {
			ExeclConfigDomain detailDomain = headerList.get(i);
			addCellGbkHeader(wb, row0, detailDomain.getView_name(), i);
		}
	}

	if (dataList != null && dataList.size() > 0) {
		if (dataList.size() > MaxSize) {
			throw new Exception("导出的数据量太大!");
		}
		for (int i = 0; i < dataList.size(); i++) {
			rownum++;
			Row row = sheet.createRow(rownum);
			Object object = dataList.get(i);
			for (int j = 0; j < headerList.size(); j++) {
				ExeclConfigDomain detailDomain = headerList.get(j);

				Cell cell = row.createCell(j);
				String  value = formatString(BeanHelper.getPropertyValue(detailDomain.getField_name(), object));
				cell.setCellValue(value);

				if(j>47 && j<=56){

					if(value != null && !"".equals(value) ){
						CellStyle cellStyle = wb.createCellStyle();
						cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("0.00"));
						cell.setCellStyle(cellStyle);
						cell.setCellValue(Double.valueOf(value));
					}
				}
			}
		}
	}
	return wb;
}

/***‘
* 设置行
* @param wb
* @param row
* @param value
* @param i
*/

public final static void addCellGbkHeader(SXSSFWorkbook wb, Row row, String value, int i) {
	Cell cell = row.createCell(i);
	cell.setCellValue(value);
}

/*** 
 * 格式化字符串
 * @param object
 * @return
 */
public static String formatString(Object object) {
	if (object instanceof Date) {
		return sdf.format(object);
	}
	if (object == null) {
		return "";
	}
	return String.valueOf(object);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值