Hi,
Thank you for considering Aspose.
gandor:
ok the alternating worked fine but i lost the gridlines. how can i keep the gridlines and assigned color to the gridlines?
Well, it is the feature of MS Excel. If you apply the background color to the rows, the grid lines will not be visible. For this you may assign border to your cells to make the gridlines. Please see the under-mentioned code to check how you can set the borders for the cells.
gandor:
Color.LightGray not applying. i tried this in applying color and it doesn’t seem to work.
System.Drawing.Color c = System.Drawing.ColorTranslator.FromHtml(“#F5F7F8”);
Well, your mentioned color is not in the default palette of Excel 2003, so you have to add this color to the palette before using it. Please see the following code in this regard,
Sample Code:
Workbook workbook = new Workbook();
Worksheet sheet = workbook.Worksheets[0];
//Adding color to the palette at 55th index
System.Drawing.Color c = System.Drawing.ColorTranslator.FromHtml("#F5F7F8");
workbook.ChangePalette(c, 55);
//Put a string value into a cell
sheet.Cells["A1"].PutValue("Import an object Array");
//the font text is set to bold
sheet.Cells["A1"].Style.Font.IsBold = true;
//Create an object array and fill it with some values
object[] obj = { "Tom", "John", "kelly", 1, 2, 2.8, 5.16, true, false };
//Import the object array to the sheet cells
sheet.Cells.ImportObjectArray(obj, 1, 0, false);
//Autofit all the columns in the sheet
sheet.AutoFitColumns();
//Create first style i.e.., yellow shading color with red font color.
Style style1 = workbook.Styles[workbook.Styles.Add()];
style1.ForegroundColor = Color.Yellow;
style1.Pattern = BackgroundType.Solid;
style1.Borders[BorderType.TopBorder].LineStyle = CellBorderType.Thin;
style1.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Thin;
style1.Borders[BorderType.LeftBorder].LineStyle = CellBorderType.Thin;
style1.Borders[BorderType.RightBorder].LineStyle = CellBorderType.Thin;
style1.Borders[BorderType.TopBorder].Color = c;
style1.Borders[BorderType.BottomBorder].Color = c;
style1.Borders[BorderType.LeftBorder].Color = c;
style1.Borders[BorderType.RightBorder].Color = c;
style1.Font.Color = Color.Red;
StyleFlag flag1 = new StyleFlag();
flag1.CellShading = true;
flag1.FontColor = true;
flag1.Borders = true;
//Create second style i.e.., green shading color with white text color.
Style style2 = workbook.Styles[workbook.Styles.Add()];
style2.ForegroundColor = Color.Green;
style2.Pattern = BackgroundType.Solid;
style2.Borders[BorderType.TopBorder].LineStyle = CellBorderType.Thin;
style2.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Thin;
style2.Borders[BorderType.LeftBorder].LineStyle = CellBorderType.Thin;
style2.Borders[BorderType.RightBorder].LineStyle = CellBorderType.Thin;
style2.Borders[BorderType.TopBorder].Color = c;
style2.Borders[BorderType.BottomBorder].Color = c;
style2.Borders[BorderType.LeftBorder].Color = c;
style2.Borders[BorderType.RightBorder].Color = c;
style2.Font.Color = Color.White;
StyleFlag flag2 = new StyleFlag();
flag2.CellShading = true;
flag2.FontColor = true;
flag2.Borders = true;
for (int i = 0; i < 100; i++)
{
if (i % 2 == 0)
{
sheet.Cells.ApplyRowStyle(i, style1, flag1);
}
else
{
sheet.Cells.ApplyRowStyle(i, style2, flag2);
}
}
workbook.Save("c:\\alternativerowsstyles.xls");
Also, please see the following online document regarding colors and palettes,
https://docs.aspose.com/display/cellsnet/Cells+Formatting
Thank You & Best Regards,