- 1 public void exportExcel(String title, JSONArray jsonArray, List > eventRecordMapList, 2 OutputStream out) {
- 3 // 创建一个excel工作簿
- 4 HSSFWorkbook workbook = new HSSFWorkbook();
- 5 HSSFSheet sheet = workbook.createSheet(title);
- 6 // excel列默认宽度
- 7 sheet.setDefaultColumnWidth(20);
- 8 9 // 第一行标题样式(白字蓝底)
- 10 HSSFCellStyle titleStyle = workbook.createCellStyle();
- 11 HSSFPalette palette = workbook.getCustomPalette();
- 12 palette.setColorAtIndex((short) 63, (byte)(50), (byte)(126), (byte)(179));
- 13 titleStyle.setFillForegroundColor((short) 63);
- 14 titleStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
- 15 titleStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
- 16 titleStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
- 17 titleStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
- 18 titleStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
- 19 titleStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
- 20 HSSFFont font = workbook.createFont();
- 21 font.setColor(HSSFColor.WHITE.index);
- 22 font.setFontHeightInPoints((short) 12);
- 23 font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
- 24 titleStyle.setFont(font);
- 25 titleStyle.setWrapText(false);
- 26 27 // 内容行样式 (白底黑字)
- 28 HSSFCellStyle contentStyle = workbook.createCellStyle();
- 29 contentStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
- 30 contentStyle.setFillForegroundColor(HSSFColor.WHITE.index);
- 31 contentStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
- 32 contentStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
- 33 contentStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
- 34 contentStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
- 35 contentStyle.setAlignment(HSSFCellStyle.ALIGN_LEFT);
- 36 contentStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
- 37 HSSFFont font2 = workbook.createFont();
- 38 font2.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
- 39 contentStyle.setFont(font2);
- 40 contentStyle.setWrapText(true);
- 41 42 HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
- 43 HSSFComment comment = patriarch.createComment(new HSSFClientAnchor(0, 0, 0, 0, (short) 4, 2, (short) 6, 5));
- 44 comment.setString(new HSSFRichTextString("数据列表"));
- 45 comment.setAuthor("csh");
- 46 47 // 填充标题,就是第一行啦
- 48 HSSFRow row = sheet.createRow(0);
- 49 row.setHeight((short) 500);
- 50
- for (short i = 0; i < jsonArray.length(); i++) {
- 51 HSSFCell cell = row.createCell(i);
- 52 cell.setCellStyle(titleStyle);
- 53 JSONObject jsonObject = jsonArray.getJSONObject(i);
- 54 HSSFRichTextString text = new HSSFRichTextString(jsonObject.getString("headerName"));
- 55 cell.setCellValue(text);
- 56
- }
- 57 58 // 填充内容行,就是除第一行外的所有行,从第二行开始
- 59
- for (int i = 0; i < eventRecordMapList.size(); i++) {
- 60 row = sheet.createRow(i + 1);
- 61 row.setHeight((short) 350);
- 62 Map eventRecordMap = eventRecordMapList.get(i);
- 63 64
- for (int j = 0; j < jsonArray.length(); j++) {
- 65 JSONObject jsonObject = jsonArray.getJSONObject(j);
- 66 HSSFCell cell = row.createCell(j);
- 67 cell.setCellStyle(contentStyle);
- 68
- try {
- 69 String textValue = eventRecordMap.get(jsonObject.getString("header"));
- 70 71
- if (textValue != null) {
- 72 Pattern p = Pattern.compile("^//d+(//.//d+)?$"); // 匹配是否是数值类型
- 73 Matcher matcher = p.matcher(textValue);
- 74
- if (matcher.matches()) {
- 75 cell.setCellValue(Double.parseDouble(textValue));
- 76
- } else {
- 77 HSSFRichTextString richString = new HSSFRichTextString(textValue);
- 78 HSSFFont font3 = workbook.createFont();
- 79 font3.setColor(HSSFColor.BLACK.index);
- 80 richString.applyFont(font3);
- 81 cell.setCellValue(richString);
- 82
- }
- 83
- }
- 84 85
- } catch(Exception e) {
- 86 e.printStackTrace();
- 87
- } finally {
- 88 89
- }
- 90
- }
- 91 92
- }
- 93
- try {
- 94 workbook.write(out); // 将excel工作簿写入到输出流中
- 95 // workbook.close();//关闭
- 96 97
- } catch(IOException e) {
- 98 e.printStackTrace();
- 99
- }
- 100 101
- }
来源: http://www.bubuko.com/infodetail-2048303.html