private void setSheetHeader(XSSFWorkbook xWorkbook, Sheet sh,String[] title) { // 设置单元格格式为文本格式XSSFDataFormat format = xWorkbook.createDataFormat();XSSFCellStyle style = xWorkbook.createCellStyle();Font font = xWorkbook.createFont();font.setBold(true);font.setFontName("微软雅黑");font.setFontHeightInPoints((short)12);style.setFont(font);style.setBorderLeft(XSSFCellStyle.BORDER_THIN);style.setBorderRight(XSSFCellStyle.BORDER_THIN);style.setBorderBottom(XSSFCellStyle.BORDER_THIN);style.setBorderTop(XSSFCellStyle.BORDER_THIN);style.setAlignment(XSSFCellStyle.ALIGN_CENTER);style.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);style.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND);style.setFillForegroundColor(new XSSFColor(new Color(183,222,232)));style.setWrapText(true); // 单元格存在 \n转义 则会换行style.setDataFormat(format.getFormat("@")); // 设置样式-数据格式为文本XSSFCellStyle cstyle = xWorkbook.createCellStyle();cstyle.setDataFormat(format.getFormat("@")); // 设置样式-数据格式为文本Row xRow0 = sh.createRow(0);for (int i = 0; i < title.length; i++) {sh.setColumnWidth(i, 20 * 300);sh.setDefaultColumnStyle(i,cstyle);Cell xCell0 = xRow0.createCell(i);xCell0.setCellStyle(style);xCell0.setCellValue(title[i]);}}
private void setSheetHeader(XSSFWorkbook workbook, Sheet sheet, String[] title) {XSSFCellStyle cellStyle = workbook.createCellStyle();XSSFFont font = workbook.createFont();font.setBold(true);font.setFontName("微软雅黑");font.setFontHeightInPoints((short) 12);cellStyle.setFont(font);cellStyle.setBorderLeft(BorderStyle.THIN);cellStyle.setBorderRight(BorderStyle.THIN);cellStyle.setBorderTop(BorderStyle.THIN);cellStyle.setBorderBottom(BorderStyle.THIN);cellStyle.setAlignment(HorizontalAlignment.CENTER);cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);// cellStyle.setFillForegroundColor(IndexedColors.AQUA.getIndex());// //新版本使用指定的颜色IndexedColors.AQUA.getIndex()cellStyle.setFillForegroundColor(new XSSFColor(new java.awt.Color(183, 222, 232)));cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);cellStyle.setWrapText(true); // 单元格存在 \n转义 则会换行Row row = sheet.createRow(0);for (int i = 0; i < title.length; ++i) {sheet.setColumnWidth(i, 20 * 300);Cell cell = row.createCell(i);cell.setCellStyle(cellStyle);cell.setCellValue(title[i]);}}