Reading Cell Background Color

Hello,

I'm trying to integrate ASPOSE.Cells in an ASP.Net application in place of Excel automation. I need to determine whether a cell's background color is either not set, white or some other color. Using Automation, this looked like this:

Object o = ((Excel.Worksheet)oWB.Sheets[sModule]).get_Range (sCell, sCell).Interior.ColorIndex;

if ((Excel.XlColorIndex)o == Excel.XlColorIndex.xlColorIndexNone)
...

I'm trying to use the following code, but my results aren't consistent:

System.Drawing.Color o = oWB.Worksheets[sModule].Cells[sCell].Style.BackgroundColor;

My question: For a given cell, how can I reliably determine whether the cell's background color is:

  • Not set
  • White
  • Something else

What are the expected ARGB values of the returned Color object for the first two cases?

Thanks

David LeBow

Hi David,

Thanks for considering Aspose.

Well, you may use ForegroundColor property instead of BackgroundColor.

you may change your line of code:

System.Drawing.Color o = oWB.Worksheets[sModule].Cells[sCell].Style.ForegroundColor;

Regards

Amjad Sahi

Aspose Nanjing Team

To check if the color is set or not, please use Style.Pattern property.

Well.... now I AM confused. I don't see from the documentation for Style.Pattern how that would answer my question about which color is used for the cell fill.

The documentation is also not entirely clear on which would be more appropriate: Style.Pattern or FillFormat.Pattern - but in either case, I don't see how I can work out which color it is.

Why ForegroundColor seems to work is also a bit of a mystery to me. What if I had a background fill and a special foreground (text) color? What is BackgroundColor for if not to get, well, the background color?

When you format a cell in MS Excel, you can set the background pattern and background color.

Actually two colors can be set to a cell: foreground color and background color. If background pattern is solid, only foreground color will take effort.

For text color, you can use Style.Font.Color to set it.