想要 通过 自定义RGB 来设置 导出 excel 的表头颜色
pom 依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.2.7</version>
</dependency>
相关示例代码
ExcelUtils 工具类中可以本地测试
public class ComplexHeadStyles {
private Integer x;
private Integer y;
private Short indexColor;
private Integer red;
private Integer green;
private Integer blue;
public ComplexHeadStyles(Integer x, Integer y, Short indexColor) {
this.x = x;
this.y = y;
this.indexColor = indexColor;
}
public ComplexHeadStyles(Integer x, Integer y, Integer red, Integer green, Integer blue) {
this.x = x;
this.y = y;
this.red = red;
this.green = green;
this.blue = blue;
}
public Integer getX() {
return x;
}
public Integer getY() {
return y;
}
public Short getIndexColor() {
return indexColor;
}
public Integer getRed() {
return red;
}
public Integer getGreen() {
return green;
}
public Integer getBlue() {
return blue;
}
}
public class CustomHeadWriteHandler extends AbstractCellStyleStrategy {
private ArrayBlockingQueue<ComplexHeadStyles> headStylesQueue;
public CustomHeadWriteHandler(ArrayBlockingQueue<ComplexHeadStyles> headStylesQueue) {
this.headStylesQueue = headStylesQueue;
}
@Override
public void afterCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {
if (isHead) {
if (headStylesQueue != null && !headStylesQueue.isEmpty()) {
ComplexHeadStyles complexHeadStyle = headStylesQueue.peek();
Sheet sheet = writeSheetHolder.getSheet();
Workbook workbook = sheet.getWorkbook();
CellStyle cellStyle = initCellStyleCustom(workbook);
if (cell.getColumnIndex() == complexHeadStyle.getY() && relativeRowIndex.equals(complexHeadStyle.getX())) {
if (ObjectUtil.isNotEmpty(complexHeadStyle.getIndexColor())) {
cellStyle.setFillForegroundColor(complexHeadStyle.getIndexColor());
cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
cell.setCellStyle(cellStyle);
} else {
if (ObjectUtil.isNotEmpty(complexHeadStyle.getRed()) && ObjectUtil.isNotEmpty(complexHeadStyle.getGreen()) && ObjectUtil.isNotEmpty(complexHeadStyle.getBlue())) {
XSSFCellStyle xssfCellStyle = (XSSFCellStyle)workbook.createCellStyle();
xssfCellStyle.cloneStyleFrom(cellStyle);
xssfCellStyle.setFillForegroundColor(new XSSFColor(new java.awt.Color(complexHeadStyle.getRed(), complexHeadStyle.getGreen(), complexHeadStyle.getBlue())));
xssfCellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
cell.setCellStyle(xssfCellStyle);
}
}
headStylesQueue.poll();
}
}
}
}
@Override
protected void initCellStyle(Workbook workbook) {
}
@Override
protected void setHeadCellStyle(Cell cell, Head head, Integer relativeRowIndex) {
}
@Override
protected void setContentCellStyle(Cell cell, Head head, Integer relativeRowIndex) {
}
private CellStyle initCellStyleCustom(Workbook workbook) {
CellStyle style = workbook.createCellStyle();
style.setBorderBottom(BorderStyle.THIN);
style.setBorderLeft(BorderStyle.THIN);
style.setBorderTop(BorderStyle.THIN);
style.setBorderRight(BorderStyle.THIN);
Font font = workbook.createFont();
font.setFontName("微软雅黑");
font.setBold(false);
font.setFontHeightInPoints((short)13);
style.setFont(font);
return style;
}
}
public class ExcelUtils {
public static WriteHandler setExcelStyle() {
WriteCellStyle headWriteCellStyle = new WriteCellStyle();
headWriteCellStyle.setFillForegroundColor(IndexedColors.ORANGE.getIndex());
WriteFont headWriteFont = new WriteFont();
headWriteFont.setColor(IndexedColors.WHITE.getIndex());
headWriteFont.setFontName("微软雅黑");
headWriteFont.setFontHeightInPoints((short)13);
headWriteCellStyle.setWriteFont(headWriteFont);
WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
WriteFont contentWriteFont = new WriteFont();
contentWriteFont.setFontName("微软雅黑");
contentWriteFont.setFontHeightInPoints((short)11);
contentWriteFont.setColor(IndexedColors.BLACK.getIndex());
contentWriteCellStyle.setWriteFont(contentWriteFont);
return new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);
}
public static OutputStream getOutputStream(String fileName, HttpServletResponse response) throws Exception {
try {
fileName = URLEncoder.encode(fileName, "utf-8");
response.setCharacterEncoding("utf8");
response.setContentType("multipart/form-data");
response.setHeader("Content-Disposition", "attachment;filename*=utf-8'zh_cn'" + fileName + ".xlsx");
return response.getOutputStream();
} catch (IOException e) {
throw new Exception("导出excel表格失败!", e);
}
}
public static WriteHandler setTestTemplateSheet0() {
ArrayBlockingQueue<ComplexHeadStyles> complexHeadStylesArrayBlockingQueue = new ArrayBlockingQueue<>(3);
complexHeadStylesArrayBlockingQueue.add(new ComplexHeadStyles(0, 0, 102, 255, 255));
complexHeadStylesArrayBlockingQueue.add(new ComplexHeadStyles(0, 1, IndexedColors.ORANGE.getIndex()));
complexHeadStylesArrayBlockingQueue.add(new ComplexHeadStyles(0, 2, IndexedColors.GREY_25_PERCENT.getIndex()));
CustomHeadWriteHandler headStyleWriteHandler = new CustomHeadWriteHandler(complexHeadStylesArrayBlockingQueue);
return headStyleWriteHandler;
}
public static List<List<String>> getTestHeader0(List<List<Object>> list0) {
List<List<String>> list = Lists.newArrayList();
List<String> head0 = Lists.newArrayList("第一列");
List<String> head1 = Lists.newArrayList("第二列");
List<String> head2 = Lists.newArrayList("第三列");
list.add(head0);
list.add(head1);
list.add(head2);
List<Object> row1 = Lists.newArrayList("111", "222", "333");
list0.add(row1);
return list;
}
public static ByteArrayOutputStream getExcelTestOut(HttpServletResponse response) {
ExcelWriter excelWriter = null;
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
if (ObjectUtil.isNotEmpty(response)) {
excelWriter = EasyExcel.write(getOutputStream("test", response)).registerWriteHandler(setExcelStyle()).build();
} else {
excelWriter = EasyExcel.write(out).registerWriteHandler(setExcelStyle()).build();
}
List<List<Object>> list0 = Lists.newArrayList();
List<List<String>> header0 = getTestHeader0(list0);
WriteSheet sheet0 = EasyExcel.writerSheet(0, "test").head(header0).registerWriteHandler(ExcelUtils.setTestTemplateSheet0()).build();
excelWriter.write(list0, sheet0);
} catch (Exception e) {
e.printStackTrace();
} finally {
excelWriter.finish();
}
return out;
}
public static void main(String[] args) throws IOException {
ByteArrayOutputStream excelTestOut = getExcelTestOut(null);
File file = new File("D:\\test.xlsx");
if (file.exists()) {
System.out.println("文件已存在");
return;
}
file.createNewFile();
FileOutputStream fileOutputStream = new FileOutputStream(file.getPath());
fileOutputStream.write(excelTestOut.toByteArray());
fileOutputStream.close();
}
}