How to get Excel cell fill pattern using c#?
Within an Excel file, how do I go about retrieving a cell's fill pattern? Based on whether a cell has a fill pattern, I want to populate data. Below is what I have tried so far, but "xlPatternNone" is always returned no matter what the cell looks like. Thanks.
Excel.Worksheet workSheet = (Excel.Worksheet)workBook.Sheets[sheetName]; Excel.Range cell = (Excel.Range)workSheet.Range[column + row.ToString()]; if ((Excel.XlPattern)((Excel.Style)cell.Style).Interior.Pattern == Excel.XlPattern.xlPatternNone) cell.Value = value;
Info: Excel 2007, c#, Microsoft Interop libraries
This definitely works:
var workSheet = (Worksheet) workBook.Sheets["Sheet1"]; var cell = workSheet.Range["A1"]; var style = (XlPattern) cell.Interior.Pattern;
I'd therefore suggest that you ignore the Style property on the cell and just access the cell's Interior directly. If that doesn't work, I'd suggest making sure that column + row.ToString() is definitely pointing at the correct cell.