背景
在Java开发中,我们经常需要处理Excel文件,其中颜色设置是一个常见需求。Apache POI提供了IndexedColors 枚举来表示Excel中的标准颜色。本文将展示如何生成一个包含所有标准颜色的Excel文件,并提供对应的十六进制颜色值和RGB值。
Java代码实现
package com.yzycoc.hamster.demo.time202505.time20250528.test;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.FillPatternType;
import java.util.ArrayList;
import java.util.List;
public class ColorExcelGenerator {
public static void main(String[] args) {
String fileName = "颜色代码列表.xlsx";
// 1. 生成颜色数据
List<ColorInfo> colorList = new ArrayList<>();
for (IndexedColors color : IndexedColors.values()) {
colorList.add(new ColorInfo(color.name(), color.getIndex()));
}
// 2. 生成Excel并写入
EasyExcel.write(fileName, ColorInfo.class)
.registerWriteHandler(new HorizontalCellStyleStrategy(null, generateContentStyles()))
.sheet("颜色列表")
.doWrite(colorList);
System.out.println("Excel生成完成:" + fileName);
}
// 数据类
public static class ColorInfo {
@ExcelProperty("颜色名称")
private String colorName;
@ExcelProperty("颜色代码")
private Short colorIndex;
public ColorInfo(String colorName, short colorIndex) {
this.colorName = colorName;
this.colorIndex = colorIndex;
}
public String getColorName() {
return colorName;
}
public Short getColorIndex() {
return colorIndex;
}
}
// 设置每个颜色对应的单元格背景色
private static List<WriteCellStyle> generateContentStyles() {
List<WriteCellStyle> styles = new ArrayList<>();
for (IndexedColors color : IndexedColors.values()) {
WriteCellStyle style = new WriteCellStyle();
style.setFillForegroundColor(color.getIndex());
style.setFillPatternType(FillPatternType.SOLID_FOREGROUND);
WriteFont font = new WriteFont();
font.setFontHeightInPoints((short) 10);
font.setColor(IndexedColors.BLACK.getIndex());
style.setWriteFont(font);
styles.add(style);
}
return styles;
}
}
Excel 颜色代码对照表
| 颜色名称 | 颜色代码 | 十六进制值 | RGB值 | 颜色示例 |
|---|---|---|---|---|
| BLACK | 8 | #000000 | 0,0,0 | ![]() |
| WHITE | 9 | #FFFFFF | 255,255,255 | ![]() |
| RED | 10 | #FF0000 | 255,0,0 | ![]() |
| BRIGHT_GREEN | 11 | #00FF00 | 0,255,0 | ![]() |
| BLUE | 12 | #0000FF | 0,0,255 | ![]() |
| YELLOW | 13 | #FFFF00 | 255,255,0 | ![]() |
| PINK | 14 | #FF00FF | 255,0,255 | ![]() |
| TURQUOISE | 15 | #00FFFF | 0,255,255 | ![]() |
| DARK_RED | 16 | #800000 | 128,0,0 | ![]() |
| GREEN | 17 | #008000 | 0,128,0 | ![]() |
| DARK_BLUE | 18 | #000080 | 0,0,128 | ![]() |
| DARK_YELLOW | 19 | #808000 | 128,128,0 | ![]() |
| VIOLET | 20 | #800080 | 128,0,128 | ![]() |
| TEAL | 21 | #008080 | 0,128,128 | ![]() |
| GREY_25_PERCENT | 22 | #C0C0C0 | 192,192,192 | ![]() |
| GREY_50_PERCENT | 23 | #808080 | 128,128,128 | ![]() |
| CORNFLOWER_BLUE | 24 | #9999FF | 153,153,255 | ![]() |
| MAROON | 25 | #7F0000 | 127,0,0 | ![]() |
| LEMON_CHIFFON | 26 | #FFFF99 | 255,255,153 | ![]() |
| ORCHID | 28 | #660066 | 102,0,102 | ![]() |
| CORAL | 29 | #FF8080 | 255,128,128 | ![]() |
| ROYAL_BLUE | 30 | #0066CC | 0,102,204 | ![]() |
| LIGHT_CORNFLOWER_BLUE | 31 | #CCFFFF | 204,255,255 | ![]() |
颜色使用说明
- 十六进制值:适用于HTML/CSS,格式为
#RRGGBB - RGB值:适用于图形软件,格式为
R,G,B(每个值0-255) - 颜色代码:Excel中使用的索引值
提示:点击颜色示例可以查看大图,所有颜色预览使用placeholder.com生成
























评论区