您的位置:首页 > 编程语言 > Java开发

Excel POI导出excel

2016-08-26 11:19 281 查看
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]);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  excel poi java