We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

style.BackgroundColor got

I want to use Range.SetStyle method. I first initial a new style class by Style style =new Style(). And then, I fill in each element I need, but when I set the style.BackgroundColor = Color.Black, it got "Object reference not set to an instance of an object", what's the problem?

Another question is, I only got the color index, how I can use it to set the style.BackgroundColor?

Hi there,

Thank you for contacting Aspose support.

Please use the following approach to create a Style object. This way, the new Style object will be added to the Workbook’s Style collection. In case the problem persists, it could be a bug in your current version of the API. In that case, we would suggest you to download and use the latest version of Aspose.Cells for .NET


var book = new Workbook(file);
var style = book.Styles[book.Styles.Add()];

Sorry for not understanding other part of your inquiry, could you please elaborate it further so we could assist you better in this regard.


Please try our latest version as Babar shared the link. And, please use Style.ForegroundColor instead of BackgroundColor attribute.

1) Please see the sample code for your complete reference on how to create Style with formattings and apply style to a range of cells accordingly.
Sample code:

Workbook workbook = new Workbook();
Worksheet worksheet = workbook.Worksheets[0];
Cells cells = worksheet.Cells;
//Input some data into the cells.
for (int i = 3; i < 101; i++)
for (int j = 0; j < 26; j++)
cells[i, j].PutValue(CellsHelper.CellIndexToName(i, j));



//Define first style object
Style style = workbook.Styles[workbook.Styles.Add()];
style.Font.Color = Color.White;
style.Font.IsBold = true;
style.ForegroundColor = Color.Black;
style.Pattern = BackgroundType.Solid;
StyleFlag flag1 = new StyleFlag();
flag1.FontColor = true;
flag1.FontBold = true;
flag1.CellShading = true;

//Define second style object.
Style newstyle = workbook.Styles[workbook.Styles.Add()];
newstyle.Font.Color = Color.Blue;
newstyle.ForegroundColor = Color.Pink;
newstyle.Pattern = BackgroundType.Solid;

int irow = 3;
//Create a range.
Range range = cells.CreateRange(“A1:Z100”);
range.Name = “MyRange”;

Range range1 = workbook.Worksheets.GetRangeByName(“MyRange”);
range[0, 0].PutValue(“Testing…”);

//Apply the style on the fourth row only
cells.Rows[irow].ApplyStyle(style, flag1);
//Save the Excel file

2) Please see the sample code on how to specify color to the cells based on the color indexes in the array for your reference, please refer to it:
Sample code:

Workbook workbook = new Workbook(FileFormatType.Xlsx);

Worksheet worksheet = workbook.Worksheets[0];
Cells cells = worksheet.Cells;

//Get the color palette of the workbook
Color[] colors = workbook.Colors;


//Set the fill color based on the color indexes in the array.
for (int i = 0; i < colors.Length; i++)
Cell cell = cells[i, 0];
Style style = cell.GetStyle();
style.ForegroundColor = colors[i];
style.Pattern = BackgroundType.Solid;


Hope, this helps a bit.

Thank you.