Free Support Forum - aspose.com

Need code to set Background colour of 1st row in excel

Hi,

Thanks to ASPOSE.

While exportint to excel, I need to set backgroundcolor of 1st row to RED.
I tried in all the ways mentioned but none works, I dont know where is the mistake.
Could you pls send me the code to set the background colour of 1st row in a excel file?

Also, While setting Forecolour of a font, the formatting is not applied for DateTime datatype columns ( ws.Cells.Columns[11].Style.Number = 15; ). May I know why this happens?

Thanks in advance.

Regards,
Venkat Raj @ VRaj

Hi VRaj,

Both Row.Style and Column.Style are only for reading.

Please use Cells.ApplyRowStyle and Cells.ApplyColumnStyle method.See following codes:

Workbook workbook = new Workbook();
Cells cells = workbook.Worksheets[0].Cells;
StyleFlag styleFlag = new StyleFlag();
styleFlag.NumberFormat = true;
Style style = cells.Columns[0].Style;
style.Number = 15;
cells.ApplyColumnStyle(0, style, styleFlag);
styleFlag = new StyleFlag();
styleFlag.CellShading = true;
style = cells.Rows[0].Style;
style.Pattern = BackgroundType.Solid;
style.ForegroundColor = System.Drawing.Color.Red;
cells.ApplyRowStyle(0, style, styleFlag);

Thankyou for the code Warren.
Even I have tried the same method which you mentioned.
But yet I am getting a erro

Error 2 ‘Aspose.Cells.Cells’ does not contain a definition for ‘ApplyColumnStyle’

I just pasted your code and dint modify it.


Right now,
I am able to set the Fore color of the row, But I cant able to set the Background color of the row.

AND

Forecolor of the row is applying for all columns except DATETIME datatype columns that I have declared as.
ws.Cells.Columns[6].Style.Number = 15; But datetime value is converted fine.

This converts fine the datetime, but not applying color formatting !!!

Hi,

It seems that you are still using some older version of the component as ApplyRowStyle and ApplyColumnStyle are the latest apis.

Please try the code given by Simon with the attached version.

And for setting background color patterns, please check: http://www.aspose.com/documentation/file-format-components/aspose.cells-for-.net-and-java/colors-background-patterns.html

Thank you.