侧边栏壁纸
  • 累计撰写 32 篇文章
  • 累计创建 9 个标签
  • 累计收到 3 条评论

目 录CONTENT

文章目录

Apache POI IndexedColors 颜色代码参考

仓鼠
2025-05-28 / 0 评论 / 0 点赞 / 186 阅读 / 8134 字 / 正在检测是否收录...

背景

在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值颜色示例
BLACK8#0000000,0,0#000000
WHITE9#FFFFFF255,255,255#FFFFFF
RED10#FF0000255,0,0#FF0000
BRIGHT_GREEN11#00FF000,255,0#00FF00
BLUE12#0000FF0,0,255#0000FF
YELLOW13#FFFF00255,255,0#FFFF00
PINK14#FF00FF255,0,255#FF00FF
TURQUOISE15#00FFFF0,255,255#00FFFF
DARK_RED16#800000128,0,0#800000
GREEN17#0080000,128,0#008000
DARK_BLUE18#0000800,0,128#000080
DARK_YELLOW19#808000128,128,0#808000
VIOLET20#800080128,0,128#800080
TEAL21#0080800,128,128#008080
GREY_25_PERCENT22#C0C0C0192,192,192#C0C0C0
GREY_50_PERCENT23#808080128,128,128#808080
CORNFLOWER_BLUE24#9999FF153,153,255#9999FF
MAROON25#7F0000127,0,0#7F0000
LEMON_CHIFFON26#FFFF99255,255,153#FFFF99
ORCHID28#660066102,0,102#660066
CORAL29#FF8080255,128,128#FF8080
ROYAL_BLUE30#0066CC0,102,204#0066CC
LIGHT_CORNFLOWER_BLUE31#CCFFFF204,255,255#CCFFFF

颜色使用说明

  1. 十六进制值:适用于HTML/CSS,格式为#RRGGBB
  2. RGB值:适用于图形软件,格式为R,G,B(每个值0-255)
  3. 颜色代码:Excel中使用的索引值

提示:点击颜色示例可以查看大图,所有颜色预览使用placeholder.com生成

1748423561740.jpg

0

评论区