Excel POI导出excel

public static String[] excelTemplate1 = new String[]{};
	
	
	public static ByteArrayOutputStream getExcelFile(JSONArray data,String sheetName,String templateName){
		if(data==null||data.size()==0){
			return null;
		}
		String[] template = chooseTemplate(templateName);
		HSSFWorkbook workBook = new HSSFWorkbook();
		HSSFSheet sheet = workBook.createSheet(sheetName);
		sheet.setDefaultColumnWidth(18);
		HSSFRow tableHead = sheet.createRow(0);
		tableHead.setHeight((short)430);
        for(int i=0;i<template.length;i++){
        	String headContext = template[i].split(",")[0];
        	HSSFCell cell = tableHead.createCell(i,HSSFCell.CELL_TYPE_STRING);
        	cell.setCellStyle(getHeadStyle(workBook));
        	cell.setCellValue(headContext);
        }
        
	    for(int i=0;i<data.size();i++){
	    	JSONObject jsonData = data.getJSONObject(i);
	    	HSSFRow row = sheet.createRow(i+1);
	    	row.setHeight((short)350);
	    	for(int j=0;j<template.length;j++){
	    		String key = template[j].split(",")[1];
	    		//String value = jsonData.getString(key);
	    		String value = parseData(jsonData, key);
	            String type = template[j].split(",")[2];
	            if(j==0){
	            	setCellValue(row, j, value, type,getFirstCellStyle(workBook));
	            }else{
	            	setCellValue(row, j, value, type,getBodyStyle(workBook));
	            }
	    	}
	    }
	    ByteArrayOutputStream bos = new ByteArrayOutputStream();
	    try {
			workBook.write(bos);
		} catch (IOException e) {
			throw new RuntimeException("export excel error");
		}
		return bos;
	}
	
	private static String[] chooseTemplate(String templateName){
		String[] res = null;
		switch(templateName){
			case "excelTemplate1":
				res = excelTemplate1;
				break;
		}
		return res;
	}
	
	private static void setCellValue(HSSFRow row,int index,String value,String type,HSSFCellStyle style){
		HSSFCell cell = null;
		switch(type){
			case "0":
				cell = row.createCell(index,HSSFCell.CELL_TYPE_NUMERIC);
				if(value==null){
					cell.setCellValue("-");
				}else{
					cell.setCellValue(Double.valueOf(value));
				}
				cell.setCellStyle(style);
				break;
			case "1":
				cell = row.createCell(index,HSSFCell.CELL_TYPE_STRING);
				cell.setCellValue(value==null?"-":value);
				cell.setCellStyle(style);
				break;
			case "2":
				cell = row.createCell(index,HSSFCell.CELL_TYPE_NUMERIC);
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
				cell.setCellValue(value==null?"-":sdf.format(new Date(Long.valueOf(value))));
				cell.setCellStyle(style);
				break;
		}
	}
	
	private static HSSFCellStyle getHeadStyle(HSSFWorkbook workBook){
		HSSFCellStyle centerStyle = workBook.createCellStyle();
		HSSFFont font = workBook.createFont();
		font.setFontHeightInPoints((short) 12); // 字体高度
		font.setFontName(" 黑体 "); // 字体
		font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
		centerStyle.setFont(font);
		centerStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		centerStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
		centerStyle.setFillForegroundColor(IndexedColors.SEA_GREEN.getIndex());
		centerStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
		return centerStyle;
	}
	
	private static HSSFCellStyle getBodyStyle(HSSFWorkbook workBook){
		HSSFCellStyle centerStyle = workBook.createCellStyle();
		HSSFFont font = workBook.createFont();
		font.setFontHeightInPoints((short) 10); // 字体高度
		centerStyle.setFont(font);
		centerStyle.setAlignment(HSSFCellStyle.ALIGN_RIGHT);
		centerStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
		return centerStyle;
	}
	
	private static HSSFCellStyle getFirstCellStyle(HSSFWorkbook workBook){
		HSSFCellStyle centerStyle = workBook.createCellStyle();
		HSSFFont font = workBook.createFont();
		font.setFontHeightInPoints((short) 10); // 字体高度
		centerStyle.setFont(font);
		centerStyle.setAlignment(HSSFCellStyle.ALIGN_LEFT);
		centerStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
		centerStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
		centerStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
		return centerStyle;
	}
	
	private static String parseData(JSONObject json,String key){
		JSONObject temp = null;
		if(!key.contains(".")){
			return json.getString(key);
		}else{
			String[] keys = key.split("\\.");
			for(int i=0;i<keys.length-1;i++){
				temp = json.getJSONObject(keys[i]);
			}
			return temp.getString(keys[keys.length-1]);
		}	
	}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值