Hi,
Please try our latest version as Babar shared the link. And, please use Style.ForegroundColor instead of BackgroundColor attribute.
1) Please see the sample code for your complete reference on how to create Style with formattings and apply style to a range of cells accordingly.
e.g
Sample code:
Workbook workbook = new Workbook();
Worksheet worksheet = workbook.Worksheets[0];
Cells cells = worksheet.Cells;
//Input some data into the cells.
for (int i = 3; i < 101; i++)
{
for (int j = 0; j < 26; j++)
{
cells[i, j].PutValue(CellsHelper.CellIndexToName(i, j));
}
}
//Define first style object
Style style = workbook.Styles[workbook.Styles.Add()];
style.Font.Color = Color.White;
style.Font.IsBold = true;
style.ForegroundColor = Color.Black;
style.Pattern = BackgroundType.Solid;
StyleFlag flag1 = new StyleFlag();
flag1.FontColor = true;
flag1.FontBold = true;
flag1.CellShading = true;
//Define second style object.
Style newstyle = workbook.Styles[workbook.Styles.Add()];
newstyle.Font.Color = Color.Blue;
newstyle.ForegroundColor = Color.Pink;
newstyle.Pattern = BackgroundType.Solid;
int irow = 3;
//Create a range.
Range range = cells.CreateRange(“A1:Z100”);
range.Name = “MyRange”;
Range range1 = workbook.Worksheets.GetRangeByName(“MyRange”);
range.SetStyle(newstyle);
range[0, 0].PutValue(“Testing…”);
//Apply the style on the fourth row only
cells.Rows[irow].ApplyStyle(style, flag1);
//Save the Excel file
workbook.Save(“e:\test2\outrangestyle1.xlsx”);
2) Please see the sample code on how to specify color to the cells based on the color indexes in the array for your reference, please refer to it:
e.g
Sample code:
Workbook workbook = new Workbook(FileFormatType.Xlsx);
Worksheet worksheet = workbook.Worksheets[0];
Cells cells = worksheet.Cells;
//Get the color palette of the workbook
Color[] colors = workbook.Colors;
MessageBox.Show(colors.Length.ToString());
//Set the fill color based on the color indexes in the array.
for (int i = 0; i < colors.Length; i++)
{
Cell cell = cells[i, 0];
Style style = cell.GetStyle();
style.ForegroundColor = colors[i];
style.Pattern = BackgroundType.Solid;
cell.SetStyle(style);
}
workbook.Save(“e:\test2\outcolorsstyle1.xlsx”);
Hope, this helps a bit.
Thank you.