使用Poi在Excel中获取单元格颜色 [英] Get Cell color in excel with poi

查看:1510
本文介绍了使用Poi在Excel中获取单元格颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我阅读了所有问题,但没有任何答案.如果我在excel中更改单元格颜色,我仍然会得到0和64.我正在使用excel 2007和poi 3.11.感谢您的帮助.

I read all questions on so but no answer was working. If i change the cell color in excel i still get 0 and 64. I am using excel 2007 and poi 3.11. Thanks for help.

try{ File file = new File("C:\\Test\\poi.xlsx");
    FileInputStream fis = new FileInputStream(file);
    XSSFWorkbook wb = new XSSFWorkbook(fis);
    XSSFSheet sh = wb.getSheet("Tabelle1");
    XSSFCellStyle cs = sh.getRow(0).getCell(0).getCellStyle();   
    System.out.println("Color: "+cs.getFillForegroundColor()); // 0
    System.out.println("Color: "+cs.getFillBackgroundColor()); // 64
    }catch(Exception e){e.printStackTrace();}

推荐答案

cs.getFillForegroundColorColor().getARGBHex()

这将以十六进制返回ARGB

This will return the ARGB in hex

更新
要检查颜色,您可以执行以下操作:

UPDATE
To check the color, you can do:

Color color = cs.getFillForegroundColorColor();

if (color.GREY_25_PERCENT || color.GREY_40_PERCENT || color.GREY_50_PERCENT || color.GREY_80_PERCENT)
    // is grey
}

或仅使用两种颜色:

Color color = cs.getFillForegroundColorColor();

if (color.WHITE) {
  // is white
}else {
  // is grey
}

这篇关于使用Poi在Excel中获取单元格颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆