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

Free Support Forum - aspose.com

Background color property

I am using the following code to set the background color of a cell. When I open the exported spreadsheet, the color is not set.

sheet.Cells[row, col].Style.BackgroundColor = Color.Yellow;

What am I doing wrong?

Thanks,
John

Hi John,

In your case, please set the foreground color to yellow.

sheet.Cells[row, col].Style.ForegroundColor = Color.Yellow;

Background color only takes effect when the background pattern is not solid.

@jeljeljel,
Aspose.Excel was discontinued long back and replaced with Aspose.Cells which is better in performance and contains vast range of features as compared to its predecessor. Aspose.Cells also supports formatting data, cells and applying styles on rows and columns. Here is an example of formatting a cell using this new product:

// Instantiating a Workbook object
Workbook workbook = new Workbook();

// Adding a new worksheet to the Workbook object
int i = workbook.Worksheets.Add();

// Obtaining the reference of the newly added worksheet by passing its sheet index
Worksheet worksheet = workbook.Worksheets[i];

// Define a Style and get the A1 cell style
Style style = worksheet.Cells["A1"].GetStyle();

// Setting the foreground color to yellow
style.ForegroundColor = Color.Yellow;

// Setting the background pattern to vertical stripe
style.Pattern = BackgroundType.VerticalStripe;

// Apply the style to A1 cell
worksheet.Cells["A1"].SetStyle(style);

// Get the A2 cell style
style = worksheet.Cells["A2"].GetStyle();

// Setting the foreground color to blue
style.ForegroundColor = Color.Blue;

// Setting the background color to yellow
style.BackgroundColor = Color.Yellow;

// Setting the background pattern to vertical stripe
style.Pattern = BackgroundType.VerticalStripe;

// Apply the style to A2 cell
worksheet.Cells["A2"].SetStyle(style);

// Saving the Excel file
workbook.Save(dataDir + "book1.out.xls", SaveFormat.Excel97To2003); 

You can get more details on this topic here:
Cells formatting

Here you will find the latest version of this new product which can be freely downloaded for trials:
Aspose.Cells for .NET (Latest Version)

A detailed runnable solution is prepared which can be used to test the product features without writing any code. It can be downloaded here.