Free Support Forum -

Change Cell Backcolor and ForeColor


im useing your aspose.cell version

im tring to set a cell backcolor and forecolor to differant colors

and it doent work i tried the exaples you gave in the forums

and they didnt work as well

i tried this code :

public Cell WriteCell(Worksheet sheetRef, T obj, int colIndex, int rowIndex, Style style)


string cellName = sheetRef.Cells[rowIndex, colIndex].Name;

string val = ConvertType(obj);

sheetRef.Cells[rowIndex, colIndex].PutValue(val);

sheetRef.Cells[rowIndex, colIndex].SetStyle(style);

return sheetRef.Cells[cellName];


also this code didnt work

sheetRef.Cells[rowIndex, colIndex].Style.BackgroundColor = Color.Green

can you please tell me what i did wrong ?



Well, the BackgroundColor is basically the Cells Fill Color and Foreground color is the outline color. If you use some other pattern than solid, you will see that BackgroundColor will fill the Cell and on top of that background color the ForgroundColor will be used as outline color for the pattern. As per the case of Solid Pattern, the Foreground Color will completely fill the cell, so Background color is not required then.

Since you are using some older version, we recommend you to try our latest version Aspose.Cells for .NET (Latest Version)

I use the following sample codes and they work abs fine.

Sample code:


//Instantiating a Workbook object
Workbook workbook = new Workbook();
<strong>// Add sky blue color to the palette.
//This color is not present in the standard color palette (which has 56 colors (0-55 indexed))
//so you have to add to the color palette first on some index postion.</strong>
workbook.ChangePalette(System.Drawing.Color.SkyBlue, 55);
//Obtaining the first worksheet by passing its sheet index
Worksheet worksheet = workbook.Worksheets[0];
//Now setting the background and foreground color of the "A1" cell.
worksheet.Cells["A1"].Style.BackgroundColor = Color.Yellow;
worksheet.Cells["A1"].Style.ForegroundColor = Color.SkyBlue;
//Setting the background pattern of the "A1" cell. 
worksheet.Cells["A1"].Style.Pattern = BackgroundType.ThinDiagonalStripe;
//Saving the Excel file
workbook.Save("f:\\test\\stylesfillcolors1.xls", FileFormatType.Default);


Workbook workbook = new Workbook();
Cells cells = workbook.Worksheets[0].Cells;
Style style1;
style1 = workbook.Styles[workbook.Styles.Add()];
style1.Name = "Style1";
style1.Font.Color = System.Drawing.Color.Red;
style1.ForegroundColor = Color.Green;
style1.Pattern = BackgroundType.Solid;

You can also see the following link for more details about the setting Colors & Patterns:
Cells Formatting - Colors and background patterns