package com.zzg.common.utils; import org.apache.poi.ss.usermodel.*; import org.apache.poi.ss.util.CellRangeAddress; /** * */ public class ExcelStyleUtils { /** * 设置字体 * * @param font * @param fontName * @param fontHeightInPoints */ public static void setFont(Font font, String fontName, short fontHeightInPoints) { font.setFontName(fontName); font.setFontHeightInPoints(fontHeightInPoints); } public static void setFontStyle(CellStyle style, HorizontalAlignment horizontalAlignment, VerticalAlignment verticalAlignment, FillPatternType fillPatternType) { // 设置水平居中 style.setAlignment(horizontalAlignment); // 设置垂直居中 style.setVerticalAlignment(verticalAlignment); style.setFillPattern(fillPatternType); } public static void setBorder(CellStyle style, BorderStyle border) { setBorder(style, border, border, border, border); } /** * 设置边框样式 * * @param style * @param borderTop * @param borderBottom * @param borderLeft * @param borderRight */ public static void setBorder(CellStyle style, BorderStyle borderTop, BorderStyle borderBottom, BorderStyle borderLeft, BorderStyle borderRight) { style.setBorderTop(borderTop); style.setBorderBottom(borderBottom); style.setBorderLeft(borderLeft); style.setBorderRight(borderRight); } public static void setBorderColor(CellStyle style, short color) { setBorderColor(style, color, color, color, color); } /** * 设置边框颜色 * * @param style * @param borderTop * @param borderBottom * @param borderLeft * @param borderRight */ public static void setBorderColor(CellStyle style, short borderTop, short borderBottom, short borderLeft, short borderRight) { style.setTopBorderColor(borderTop); style.setBottomBorderColor(borderBottom); style.setLeftBorderColor(borderLeft); style.setRightBorderColor(borderRight); } /** * 设置样式 */ public static void setStyle(Sheet sheet, CellRangeAddress range, CellStyle style) { for (int row = range.getFirstRow(); row <= range.getLastRow(); row++) { Row r = sheet.getRow(row); if (r == null) { r = sheet.createRow(row); } for (int col = range.getFirstColumn(); col <= range.getLastColumn(); col++) { Cell cell = r.getCell(col); if (cell == null) { cell = r.createCell(col); } cell.setCellStyle(style); } } } }