Apache POI无论单元格的背景颜色是不同的,都会返回相同的背景颜色。
创始人
2024-09-04 18:00:52
0

要解决这个问题,可以使用Apache POI的getFillForegroundColorColor()getFillBackgroundColorColor()方法来获取单元格的前景色和背景色。

下面是一个示例代码,演示如何使用Apache POI来获取单元格的前景色和背景色:

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileInputStream;
import java.io.IOException;

public class CellColorExample {
    public static void main(String[] args) {
        try {
            FileInputStream file = new FileInputStream("path/to/your/excel/file.xlsx");
            Workbook workbook = new XSSFWorkbook(file);
            Sheet sheet = workbook.getSheetAt(0);
            Row row = sheet.getRow(0);
            Cell cell = row.getCell(0);

            // 获取前景色
            CellStyle cellStyle = cell.getCellStyle();
            Color foregroundColor = cellStyle.getFillForegroundColorColor();
            System.out.println("Foreground color: " + foregroundColor);

            // 获取背景色
            Color backgroundColor = cellStyle.getFillBackgroundColorColor();
            System.out.println("Background color: " + backgroundColor);

            file.close();
            workbook.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

请将代码中的path/to/your/excel/file.xlsx替换为你的实际Excel文件的路径。

通过使用getFillForegroundColorColor()getFillBackgroundColorColor()方法,你可以获取单元格的前景色和背景色。如果单元格的前景色和背景色不同,返回的颜色对象将反映这些差异。如果单元格的前景色和背景色相同,返回的颜色对象将是相同的。

相关内容

热门资讯

透视挂!wepoker怎么发冤... 透视挂!wepoker怎么发冤家牌,其实是有挂(透视)扑克教程(有挂辅助)1、下载好wepoker怎...
透视私人局!pokerworl... 透视私人局!pokerworld修改器,一直是有挂(透视)wepoke教程(有挂规律)1、进入到po...
透视科技!hhpoker透视脚... 透视科技!hhpoker透视脚本下载,一向真的有挂(透视)揭秘教程(有挂攻略)该软件可以轻松地帮助玩...
透视线上!拱趴大菠萝机器人,确... 透视线上!拱趴大菠萝机器人,确实真的有挂(透视)玩家教程(有挂规律);1、首先打开拱趴大菠萝机器人最...
透视辅助!德普之星透视辅助软件... 透视辅助!德普之星透视辅助软件是真的吗,起初存在有挂(透视)德州教程(有挂教程)1、起透看视 德普之...
透视最新!hhpkoer辅助器... 透视最新!hhpkoer辅助器视频,从前真的有挂(透视)可靠教程(有挂细节)所有人都在同一条线上,像...
透视智能ai!aapoker插... 透视智能ai!aapoker插件下载,原先真的是有挂(透视)教你教程(有挂介绍);1、实时aapok...
透视教学!wpk模拟器多开,原... 透视教学!wpk模拟器多开,原来有挂(透视)切实教程(有挂规律)透视教学!wpk模拟器多开,原来有挂...
透视神器!pokemomo辅助... 透视神器!pokemomo辅助工具,果然是真的有挂(透视)新版2025教程(有挂工具)1、任何pok...
透视插件!pokermaste... 透视插件!pokermaster脚本,好像存在有挂(透视)AI教程(有挂黑科技)pokermaste...