Fork me on GitHub

org.apache.poi 设置 Excel 单元格颜色 RGB

org.apache.poi 设置 Excel 单元格颜色 RGB


 

一、背景说明

在使用 org.apache.poi 导出 Excel 时,需要设置部分单元格的颜色。

可以使用方法:org.apache.poi.ss.usermodel.CellStyle.setFillForegroundColor()org.apache.poi.ss.usermodel.CellStyle.setFillPattern() 来设置单元格的颜色和填充模式。

示例代码如下:

package com.miracle.excel;

import java.io.FileOutputStream;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class SetCellBackgroundColorExample {
    public static void main(String[] args) throws Exception {
        Workbook workbook = new XSSFWorkbook(); //创建Workbook对象
        Sheet sheet = workbook.createSheet("Sheet1"); //创建Sheet对象
        Row row = sheet.createRow(0); //创建行对象
        CellStyle style = workbook.createCellStyle(); //创建样式对象

        Cell cell = row.createCell(0); //创建单元格

        //设置样式的背景颜色为黄色
        style.setFillForegroundColor(IndexedColors.YELLOW.getIndex());
        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);

        cell.setCellValue("单元格颜色设置"); //设置单元格的值
        cell.setCellStyle(style); //设置单元格样式

        //将设置好格式的单元格写入Excel文件
        FileOutputStream outputStream = new FileOutputStream("D:\\example.xlsx");
        workbook.write(outputStream);
        workbook.close();
    }
}

 

导出 Excel 文件效果如下:

 如果使用 org.apache.poi  的常用版本 4.1.2, 设置颜色有局限,只能设置颜色枚举类 IndexedColors 中指定的颜色。

如果想灵活按照 RGB 颜色自由组合的方式设置单元格颜色,4.1.2 版本的 org.apache.poi 包是不支持的

 

 

二、实现方案

1、使用 5.2.3 及以上版本的 POI

如果想灵活按照 RGB 颜色自由组合的方式设置单元格颜色,必须要需要使用 5.2.3 及以上版本的 org.apache.poi 包,5.2.3以前的版本是不支持的。

 

在 pom.xml 文件中添加以下依赖:

        <!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>5.2.3</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>5.2.3</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml-schemas -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml-schemas</artifactId>
            <version>4.1.2</version>
        </dependency>

 

Maven 远程仓库地址:https://mvnrepository.com/artifact/org.apache.poi/poi

 

可以使用 XSSFColor 构造函数 来创建颜色,如下面的代码所示:

style.setFillForegroundColor(new XSSFColor(new byte[]{(byte) 255, (byte) 0, (byte) 255}, null));

这将使用RGB颜色值(255, 0, 255)来创建一个颜色对象,并将其设置为单元格的填充颜色。

 

或者使用 RGB颜色值 来设置颜色,例如:

style.setFillForegroundColor(new XSSFColor(new java.awt.Color(255,0,255), new DefaultIndexedColorMap()));

 

2、完整的代码

代码如下:

package com.miracle.excel;

import java.io.FileOutputStream;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.DefaultIndexedColorMap;
import org.apache.poi.xssf.usermodel.XSSFColor;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class SetCellBackgroundColorExample {
    public static void main(String[] args) throws Exception {
        Workbook workbook = new XSSFWorkbook(); //创建Workbook对象
        Sheet sheet = workbook.createSheet("Sheet1"); //创建Sheet对象
        Row row = sheet.createRow(0); //创建行对象
        CellStyle style = workbook.createCellStyle(); //创建样式对象

        Cell cell = row.createCell(0); //创建单元格

        //设置样式的背景颜色为黄色
//        style.setFillForegroundColor(IndexedColors.YELLOW.getIndex());
//        style.setFillForegroundColor(new XSSFColor(new byte[]{(byte) 255, (byte) 0, (byte) 255}, null));
        style.setFillForegroundColor(new XSSFColor(new java.awt.Color(255,0,255), new DefaultIndexedColorMap()));
        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);

        cell.setCellValue("单元格颜色设置"); //设置单元格的值
        cell.setCellStyle(style); //设置单元格样式

        //将设置好格式的单元格写入Excel文件
        FileOutputStream outputStream = new FileOutputStream("D:\\example.xlsx");
        workbook.write(outputStream);
        workbook.close();
    }
}

 

运行效果如下:

 

posted @ 2023-04-25 17:40  龙凌云端  阅读(1227)  评论(0编辑  收藏  举报