微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

获取 XSSF 单元格的背景颜色

如何解决获取 XSSF 单元格的背景颜色

已经有一些关于 excel 单元格的背景颜色的问题,但没有一个答案能解决我的问题。我想将颜色设为十六进制。我有一个灰色背景的单元格,但是当我使用此代码

            CellStyle cellStyle = currentRow.getCell(3).getCellStyle();
            org.apache.poi.ss.usermodel.Color color = cellStyle.getFillForegroundColorColor();
            if (color != null) {
             if (color instanceof XSSFColor) {
              System.out.println(currentRow.getCell(3).getAddress() + " XSSF Foreground: " + ((XSSFColor)color).getARGBHex());
             } else if (color instanceof hssfColor) {
              if (! (color.equals(hssfColor.hssfColorPredefined.AUTOMATIC.getColor())))
               System.out.println(currentRow.getCell(3).getAddress() + " hssf Foreground: " + ((hssfColor)color).getHexString());
             }
            }
            
            CellStyle cellStyle2 = currentRow.getCell(3).getCellStyle();
            org.apache.poi.ss.usermodel.Color color2 = cellStyle2.getFillBackgroundColorColor();
            if (color2 != null) {
             if (color2 instanceof XSSFColor) {
              System.out.println(currentRow.getCell(3).getAddress() + " XSSF Background: " + ((XSSFColor)color2).getARGBHex());
             } else if (color2 instanceof hssfColor) {
              if (! (color2.equals(hssfColor.hssfColorPredefined.AUTOMATIC.getColor())))
               System.out.println(currentRow.getCell(3).getAddress() + " hssf Background: " + ((hssfColor)color2).getHexString());
             }
            }
            
            System.out.println("Foreground Color: " + currentRow.getCell(3).getCellStyle().getFillForegroundColor());
            System.out.println("Foreground ColorColor: " + currentRow.getCell(3).getCellStyle().getFillForegroundColorColor());
            System.out.println("Background Color: " + currentRow.getCell(3).getCellStyle().getFillBackgroundColor());
            System.out.println("Background ColorColor: " + currentRow.getCell(3).getCellStyle().getFillBackgroundColorColor());
            

我明白

D70 XSSF Foreground: FFFFFFFF
D70 XSSF Background: null
Foreground Color: 0
Foreground ColorColor: org.apache.poi.xssf.usermodel.XSSFColor@8288f618
Background Color: 64
Background ColorColor: org.apache.poi.xssf.usermodel.XSSFColor@4813bcba

据我所知,背景色 64 是 AutoColor 的索引。 FFFFFFFF 不是灰色的。 任何想法如何更改代码以获得十六进制颜色?

解决方法

Excel 单元格内部有图案填充。前景色是图案的颜色,背景色是图案背后的颜色。具有颜色填充的单元格具有纯色前景图案。所以只有填充前景色才是有意义的。填充背景颜色可以忽略。

您在问题中的描述表明您已将填充前景色正确地设为 XSSFColor

但至少对于 Office Open XML (XSSF) 有多种设置颜色的可能性。它可能是一个 ARGB 十六进制值集。但它也可能是设置了色调值的主题颜色。

根据我的经验,使用 ExtendedColor.getRGBWithTint 可以获得最佳结果。

示例:

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellAddress;
import org.apache.poi.hssf.util.HSSFColor;

import java.io.FileInputStream;
import java.math.BigInteger;

class ReadExcelCellStyleFillColors {

 public static void main(String[] args) throws Exception {

  Workbook workbook = WorkbookFactory.create(new FileInputStream("./ExcelExample.xlsx"));
  //Workbook workbook = WorkbookFactory.create(new FileInputStream("./ExcelExample.xls"));

  Sheet sheet = workbook.getSheetAt(0);
  for (Row row : sheet) {
   for (Cell cell : row) {
    System.out.println("This is cell is " + new CellAddress(cell));

    CellStyle cellStyle = cell.getCellStyle();

    Color fillFGColor = cellStyle.getFillForegroundColorColor();
    System.out.println("This cell has fill foreground color:");
    System.out.println(fillFGColor);
    if (fillFGColor instanceof ExtendedColor) {
     ExtendedColor extColor = (ExtendedColor)fillFGColor;
     BigInteger bigInteger = new BigInteger(1,extColor.getRGBWithTint());
     System.out.println(bigInteger.toString(16));
    } else if (fillFGColor instanceof HSSFColor) {
     HSSFColor hssfColor = (HSSFColor)fillFGColor; 
     System.out.println(hssfColor.getHexString());
    }

    System.out.println();
   }
  }
  workbook.close();
 }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。