Free Support Forum - aspose.com

Setting Background color for cells or rows

How to set background color for cels or rows

I have used foreground color property but it is not effective if the background type "none".

so i need some way to set background color .

PLs do help its really urgent sorry for that!

Hi,

Thanks for considering Aspose.

The cell shading color would not be affected if the backgroundtype is none.

I have written some sample codes for your requirement which describes different ways to set foreground, background colors with pattern type set.

1). To set shading color to a cell.

Workbook workbook = new Workbook();
Cells cells = workbook.Worksheets[0].Cells;
Style style = cells["A1"].GetStyle();
style.ForegroundColor = Color.Red;
style.Pattern = BackgroundType.Solid;
cells["A1"].SetStyle(style);
workbook.Save("d:\\test\\cellsstyles.xls");

2). To set cell foregournd and backgournd color to a cell.

//Instantiating a Workbook object
Workbook workbook = new Workbook();
// Add sky blue color to the palette.
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("d:\\test\\stylesfillcolors.xls",FileFormatType.Default);

3). To set shading color to a row.

Workbook wb = new Workbook();
Worksheet sheet = wb.Worksheets[0];

Style style1 = wb.Styles[wb.Styles.Add()];
style1.ForegroundColor = Color.Yellow;
style1.Pattern = BackgroundType.Solid;
StyleFlag flag = new StyleFlag();
flag.CellShading = true;
sheet.Cells.ApplyRowStyle(0,style1, flag);

wb.Save("d:\\test\\stylesrow.xls", FileFormatType.ExcelXP);

For further reference, please check the following:

http://www.aspose.com/documentation/file-format-components/aspose.cells-for-.net-and-java/colors-background-patterns.html

Thank you.