I'm trying to set a range of worksheet cells to have a different background color using the following code sample:
Dim WkSht As Aspose.Cells.Worksheet
Dim CellStyle as Aspose.Cells.Style
For j = 1 to 19
CellStyle = WkSht.Cells(9,j).GetStyle
CellStyle.BackgroundColor = System.Drawing.Color.LightSalmon
WkSht.Cells(9,j).SetStyle(CellStyle)
Next j
The above code doesn't work.
Hi,
Please us ForegroundColor instead of BackgroundColor. Also, use Solid Pattern type, see your updated code:
Dim WkSht As Aspose.Cells.Worksheet
Dim CellStyle as Aspose.Cells.Style
For j = 1 to 19
CellStyle = WkSht.Cells(9,j).GetStyle
CellStyle.ForegroundColor = System.Drawing.Color.LightSalmon
CellStyle.Pattern = BackgroundType.Solid
WkSht.Cells(9,j).SetStyle(CellStyle)
Next j
For complete reference, see the document:
http://www.aspose.com/documentation/.net-components/aspose.cells-for-.net/colors-background-patterns.html
The suggested code works for Color.LightSalmon and Color.Orange but does not work for Color.Transparent, Color.White, Color.LightGray, Color.LightYellow, Color.Yellow, Color.Empty,…I got tired of testing.
Hi,
For XLS file format, if you are trying to set the custom colors that are not present on Excel’s color palette, you need to add the colors to the palette first before using them.
See the topic:
http://www.aspose.com/documentation/.net-components/aspose.cells-for-.net/colors-and-palette.html
For XLSX files, you don’t need the custom colors to added to the palette as the Excel 2007/2010 XLSX format’s palette has lots of colors.
If you still could not evaluate, kindly give us complete sample code and output files here, we will check it soon.
Thank you.
I'm bad--my row ID was incorrect in the first loop (not in the sample) and the second loop (sample code) was overwriting the changes made in the first loop.
Everything works like it should now.
Hi,
That is great as everything works fine as expected for you.
Have a good day!