You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Apache POI单元格背景色设置异常:蓝色未生效显示灰色

解决Apache POI设置Excel单元格蓝色背景显示灰色的问题

你代码里的问题核心是混淆了setFillBackgroundColor和setFillForegroundColor的作用:

  • setFillBackgroundColor控制的是填充图案底层的颜色
  • setFillForegroundColor控制的是填充图案**本身(比如点、线条)**的颜色

当你使用CellStyle.FINE_DOTS这类填充图案时,单元格呈现的主要视觉效果是图案的颜色(前景色),而你只设置了背景色,前景色默认是灰色,所以最终显示成灰色。

修正方案1:设置纯蓝色实心背景

如果想要单元格显示纯蓝色,把填充模式改成实心,同时设置前景色为蓝色:

HSSFCellStyle headerCellStyle = worksheet.getWorkbook().createCellStyle();
// 设置填充前景色为蓝色(实心模式下前景色就是单元格整体背景色)
headerCellStyle.setFillForegroundColor(HSSFColor.BLUE.index);
headerCellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);

cell = rowHeader.createCell(startColIndex++);
cell.setCellValue("Titre");
cell.setCellStyle(headerCellStyle);

修正方案2:保留FINE_DOTS图案并让点显示蓝色

如果想保留点状图案,只需把前景色设为蓝色,还可以搭配设置底层背景色:

HSSFCellStyle headerCellStyle = worksheet.getWorkbook().createCellStyle();
headerCellStyle.setFillForegroundColor(HSSFColor.BLUE.index); // 点状图案的颜色为蓝色
headerCellStyle.setFillBackgroundColor(HSSFColor.WHITE.index); // 底层背景设为白色
headerCellStyle.setFillPattern(CellStyle.FINE_DOTS);

cell = rowHeader.createCell(startColIndex++);
cell.setCellValue("Titre");
cell.setCellStyle(headerCellStyle);

内容的提问来源于stack exchange,提问作者thomas

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 05:25:30