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]); } }
相关文章推荐
- java对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- PropertyChangeListener简单理解
- 使用Python生成Excel格式的图片
- c++11 + SDL2 + ffmpeg +OpenAL + java = Android播放器
- 插入排序
- 冒泡排序