Free Support Forum - aspose.com

Change Cell Backcolor and ForeColor

Hello,

im useing your aspose.cell version 4.4.3.21

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 ?

thanks

Hi,

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:

1).

//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);

2).

Workbook workbook = new Workbook();
Cells cells = workbook.Worksheets[0].Cells;
cells["A1"].PutValue(100);
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;
cells["A1"].SetStyle(style1);
workbook.Save(@"f:\test\mystyles_test1.xls"); 

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