How to know whether background/foreground color has been set or not

Default foreground and background color in RGB always retuns 0,0,0 which is black. but in excel default background color is white. I would like to know how to check whether colors been applied to cells or not?

I am not able to differentiate between color set as black and default color. Please suggest

Regards,
Azhar

Hi Azhar,

Thank you for using Aspose products.

You can detect if a cell’s Foreground/Background color is set or not by getting the Cell’s Style object and retrieve Foreground/Background color properties accordingly. If the returned value is 0 that means no color has been set, otherwise the returned value will contain the ARGB values. Please check the below provide code snippet for your reference,

Java


Workbook workbook = new Workbook(myDir + “book1.xlsx”);
Worksheet worksheet = workbook.getWorksheets().get(0);

Cell cell = worksheet.getCells().get(“D2”);
Style style = cell.getDisplayStyle();
Color BackgroundColor = style.getBackgroundColor();
Color ForegroundColor = style.getForegroundColor();

System.out.println(BackgroundColor);
System.out.println(ForegroundColor);

Please note, you can directly fetch the Cell's Style using the cell.getStyle() method whereas cell.getDisplayStyle() method is useful in a case where conditional formatting is applied to a cell.

Please feel free to write back if you have more questions.

Thanks for your prompt reply. I think I haven’t put my question properly. Here is the scenario :

consider I don’t have any background set, then style.getBackgroundColor() will return me 0,0,0 in RGB. it means default background color is black.

Now , I set the background color as Black then
style.getBackgroundColor() will return me again 0,0,0 in RGB.

How do I differentiate now? The returned black color is default color or its explicitly set by the user.

I hope I am clear.

Regards,
Azhar


Hi Azhar,

Thanks for your posting and using Aspose.Cells.

When the color is explicitly set by the user, then the Style.getPattern() property returns BackgroundType.SOLID. If it returns BackgroundType.NONE, then the returned colors of Style object are invalid. So, you can use the following code to check the valid fill color of the cell

Java


String filePath = “F:\Shak-Data-RW\Downloads\source.xlsx”;


Workbook workbook = new Workbook(filePath);


Worksheet worksheet = workbook.getWorksheets().get(0);


Cell cell = worksheet.getCells().get(“A1”);


Style style = cell.getStyle();


System.out.println(style.getPattern());


if(style.getPattern() == BackgroundType.SOLID)

{

System.out.println("[Valid Fill Color]: " + style.getForegroundColor());

}


Output:
[Valid Fill Color]: com.aspose.cells.Color@ffff00

Thanks… that worked.

Regards,
Azhar

Below code doesn’t work. I have a check to see whether background color has been applied. so I am depending on style.getPattern() method. but that method return 0 even if background color has been set by the user. Here is code snippet.


Cell cell = sheet.getCells().get(“A1”);
style = cell.getStyle();
style.setForegroundColor(Color.getGreen());
color = style.getForegroundColor();
System.out.println(“pattern:”+style.getPattern());
System.out.println(“background color : “+color.getR()+”,”+color.getG()+","+color.getB() );
color = style.getFont().getColor();
System.out.println(“foreground color : “+color.getR()+”,”+color.getG()+","+color.getB() );

Output:
pattern:0
background color : 0,-128,0
foreground color : 0,0,0

Hi,

Thanks for your posting and using Aspose.Cells.

You are not setting the Style.Pattern, therefore it is showing 0. If you will not set the Pattern as Solid Fill, then the color will not be applied to the cell in the output xls/xlsx file.

Therefore if you want to apply the color to any cell, you need to set the Style.Pattern before setting its color using Style.ForegroundColor property.

Thanks… I thought setForegroundColor() will internally do that.

Regards,
Azhar