Free Support Forum - aspose.com

Set Background Color for a whole Row in Excel worksheet in .NET

Hy there

I’m trying to set the background color for a whole row in Aspose.Cells but haven’t found a working way to do this.
I’ve searched the Documentation already but without success.

Any help would be appreciated


Hi,

I have created a sample code for your reference. May the following sample code help your need, see the code below and create your own for your need.

Sample code:

Workbook workbook = new Workbook();
Worksheet sheet = workbook.Worksheets[0];
//Put a string value into a cell
sheet.Cells["A1"].PutValue("Import an object Array");
//the font text is set to bold
sheet.Cells["A1"].Style.Font.IsBold = true;
//Create an object array and fill it with some values
object[] obj = { "Tom", "John", "kelly", 1, 2, 2.8, 5.16, true, false };
//Import the object array to the sheet cells
sheet.Cells.ImportObjectArray(obj, 1, 0, false);
//Autofit all the columns in the sheet
sheet.AutoFitColumns();

//Create first style i.e.., yellow shading color with red font color.
Style style1 = workbook.Styles[workbook.Styles.Add()];
style1.ForegroundColor = Color.Yellow;
style1.Pattern = BackgroundType.Solid;
style1.Font.Color = Color.Red;
StyleFlag flag1 = new StyleFlag();
flag1.CellShading = true;
flag1.FontColor = true;


//Create second style i.e.., green shading color with white text color.
Style style2 = workbook.Styles[workbook.Styles.Add()];
style2.ForegroundColor = Color.Green;
style2.Pattern = BackgroundType.Solid;
style2.Font.Color = Color.White;
StyleFlag flag2 = new StyleFlag();
flag2.CellShading = true;
flag2.FontColor = true;

for (int i = 0; i < 100; i++)
{

if (i % 2 == 0)
{
sheet.Cells.ApplyRowStyle(i, style1, flag1);

}
else {

sheet.Cells.ApplyRowStyle(i, style2, flag2);
}

}


workbook.Save("f:\\test\\alternativerowsstyles.xls");
For further reference, please check the doc topic:
Formatting rows and columns
If you still find any problem,do let us know.
Thank you.