1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > Springboot+POI通用Excel表格导出表头样式设置方法

Springboot+POI通用Excel表格导出表头样式设置方法

时间:2022-07-13 06:57:26

相关推荐

Springboot+POI通用Excel表格导出表头样式设置方法

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]);}}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。