Free Support Forum - aspose.com

Style and Background Color in Cells vs Aspose.Excel

I am trying to port my existing Aspose.Excel code to Aspose.Cells and I can no longer set the styles and Background color. We are looking at upgrading because I heard .Cells supports setting the style for a Range, whereas .Excel did not. I am using the following code to no avail:

Style styleTest = this._excelCalendar.Styles[this._excelCalendar.Styles.Add()];

_excelCalendar.ChangePalette(Color.LemonChiffon, 31);

styleTest.BackgroundColor = Color.LemonChiffon;
styleTest.Pattern = BackgroundType.Solid;
styleTest.HorizontalAlignment = TextAlignmentType.Center;
styleTest.VerticalAlignment = TextAlignmentType.Center;
styleTest.Font.Size = 10;
styleTest.Font.IsBold = true;
_excelCalendar.Worksheets[0].Cells[20, 20].Style = styleTest;
_excelCalendar.Worksheets[0].Cells[20, 20].PutValue("WTF????");


this._excelCalendar.Worksheets[worksheetIndex].Cells[5, 5].Style.ForegroundColor = Color.Gray;
Range rangeHoliday = this._excelCalendar.Worksheets[worksheetIndex].Cells.CreateRange(5, 5, 40, 1);
rangeHoliday.SetOutlineBorder(BorderType.LeftBorder, CellBorderType.Thin, Color.Black);
rangeHoliday.SetOutlineBorder(BorderType.RightBorder, CellBorderType.Thin, Color.Black);
rangeHoliday.SetOutlineBorder(BorderType.TopBorder, CellBorderType.Thin, Color.Black);
rangeHoliday.SetOutlineBorder(BorderType.BottomBorder, CellBorderType.Thin, Color.Black);


rangeHoliday.Style = styleTest;

PLease help.

Please change your code to:

Style styleTest = this._excelCalendar.Styles[this._excelCalendar.Styles.Add()];

_excelCalendar.ChangePalette(Color.LemonChiffon, 31);

styleTest.ForegroundColor = Color.LemonChiffon;
styleTest.Pattern = BackgroundType.Solid;
styleTest.HorizontalAlignment = TextAlignmentType.Center;
styleTest.VerticalAlignment = TextAlignmentType.Center;
styleTest.Font.Size = 10;
styleTest.Font.IsBold = true;
_excelCalendar.Worksheets[0].Cells[20, 20].Style = styleTest;
_excelCalendar.Worksheets[0].Cells[20, 20].PutValue("WTF????");


this._excelCalendar.Worksheets[worksheetIndex].Cells[4, 4].Style.ForegroundColor = Color.Gray;

this._excelCalendar.Worksheets[worksheetIndex].Cells[4, 4].Style.Pattern = BackgroundType.Solid;
Range rangeHoliday = this._excelCalendar.Worksheets[worksheetIndex].Cells.CreateRange(5, 5, 40, 1);

rangeHoliday.Style = styleTest;
rangeHoliday.SetOutlineBorder(BorderType.LeftBorder, CellBorderType.Thin, Color.Black);
rangeHoliday.SetOutlineBorder(BorderType.RightBorder, CellBorderType.Thin, Color.Black);
rangeHoliday.SetOutlineBorder(BorderType.TopBorder, CellBorderType.Thin, Color.Black);
rangeHoliday.SetOutlineBorder(BorderType.BottomBorder, CellBorderType.Thin, Color.Black);

Cell shading colors includes two colors: foreground color and background color. Only when the background pattern is not solid, background color will take effect.

Thank you for the quick response.

I have a follow up issue. In my app I set the color of a row to red. Later, I set certain columns to Gray. When I do this, the rows that intersect the column and that are shaded lose their shading. Is there any way around this? I use the following code to shade the row and column:

int worksheetIndex = 0;

Style style1 = this._excelCalendar.Styles[this._excelCalendar.Styles.Add()];

style1.Pattern = BackgroundType.Solid;

style1.ForegroundColor = Color.Teal;

style1.HorizontalAlignment = TextAlignmentType.Left;

style1.VerticalAlignment = TextAlignmentType.Center;

style1.Font.Size = 5;

Style style2 = this._excelCalendar.Styles[this._excelCalendar.Styles.Add()];

style2.Pattern = BackgroundType.Solid;

style2.ForegroundColor = Color.Red;

style2.HorizontalAlignment = TextAlignmentType.Left;

style2.VerticalAlignment = TextAlignmentType.Center;

style2.Font.Size = 5;

Range range1 = this._excelCalendar.Worksheets[worksheetIndex].Cells.CreateRange(5,0,1,30);

range1.SetOutlineBorder(BorderType.LeftBorder, CellBorderType.Thin, Color.Black);

range1.SetOutlineBorder(BorderType.RightBorder, CellBorderType.Thin, Color.Black);

range1.SetOutlineBorder(BorderType.TopBorder, CellBorderType.Thin, Color.Black);

range1.SetOutlineBorder(BorderType.BottomBorder, CellBorderType.Thin, Color.Black);

range1.Style = style1;

Range range2 = this._excelCalendar.Worksheets[worksheetIndex].Cells.CreateRange(0, 15, 30, 1);

range2.SetOutlineBorder(BorderType.LeftBorder, CellBorderType.Thin, Color.Black);

range2.SetOutlineBorder(BorderType.RightBorder, CellBorderType.Thin, Color.Black);

range2.SetOutlineBorder(BorderType.TopBorder, CellBorderType.Thin, Color.Black);

range2.SetOutlineBorder(BorderType.BottomBorder, CellBorderType.Thin, Color.Black);

range2.Style = style2;

Based on the application, I cannot write the column first. Is there a way to make the row appear on top?

If you cannot write the column first, you have to create a range on the intersect cells and assign the style again.