如何使用apache poi获取单元格的背景颜色? [英] How to get cell's background color using apache poi?

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

问题描述

我们如何获得XSSFCell背景颜色.我尝试使用 XSSFCellStyle 但没有成功.

How do we get background color of a XSSFCell. I tried using XSSFCellStyle but no luck.

FileInputStream fis = new FileInputStream(fileName);
XSSFWorkbook book = new XSSFWorkbook(fis);
XSSFSheet sheet = book.getSheetAt(0);
XSSFRow row = sheet.getRow(0);

System.out.println(row.getCell(0).getCellStyle().getFillForegroundColor());

使用这些步骤我无法获得 Short 类型的背景颜色表示.

Using these steps I am not able to get background color representation in Short type.

推荐答案

查看此 URL:

https://issues.apache.org/bugzilla/show_bug.cgi?id=45492

Cell cell = row.getCell(1);
            CellStyle cellStyle = cell.getCellStyle();          
            System.out.println("color = " + getColorPattern(cellStyle.getFillForegroundColor()));




private short[] getColorPattern(short colorIdx){        
    short[] triplet = null;
    HSSFColor color = palette.getColor(colorIdx);
    triplet = color.getTriplet();       
    System.out.println("color : " + triplet[0] +"," + triplet[1] + "," + triplet[2]);
    return triplet;
}

这将返回 RGB 代码,但不是确切的代码.但与 XLS 自定义颜色选择器中的实际颜色代码相比,它返回​​的颜色或多或少相同.

This returns the RGB codes but not exact ones. But its more or less the same color returned when compared with the actual color code in the XLS custom color picker.

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

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