Please see the following sample code with comments that will give you answers to your issues/ queries. I have also attached the output Excel file for your reference:
Workbook workbook = new Workbook();
Worksheet _worksheet = workbook.Worksheets[0];
Range RangeTitle = _worksheet.Cells.CreateRange(“A2”, “K2”);
RangeTitle.Merge();
RangeTitle.Value = “Bg”;
//Please always try to add new style like this, when you add new styles in the workbook.
Style TitleStyle = workbook.Styles[workbook.Styles.Add()];
TitleStyle.Font.Size = 20;
TitleStyle.Font.IsBold = true;
//FYI. the font’s color and cell shading color are same, I think you should change either
//font color or background color.
TitleStyle.Font.Color = ColorTranslator.FromHtml("#E9AF32");
//Set the shading/ fill color for the cells in the range.
TitleStyle.ForegroundColor = ColorTranslator.FromHtml("#E9AF32");
TitleStyle.Pattern = BackgroundType.Solid;
TitleStyle.HorizontalAlignment = TextAlignmentType.Center;
//Specify the relevant options on for StyleFlag object
StyleFlag flag = new StyleFlag();
flag.Font = true;
flag.CellShading = true;
flag.HorizontalAlignment = true;
//Apply the style to the range with respect to StyleFlag object
RangeTitle.ApplyStyle(TitleStyle, flag);
//Set the outline borders.
RangeTitle.SetOutlineBorders(CellBorderType.Medium, System.Drawing.Color.Black);
workbook.Save(“e:\test2\outRangestyles1.xlsx”);
1) It works fine and will only apply style to the A2:K2 range, see the output Excel file.
2) Please use the lines instead to specify the background color of the cells/ range:
3) It must be an issue with your DataStyle object, please check your own code on how do you set the DateTime style in your codes.
//Instantiate a new Workbook.
Workbook workbook = new Workbook();
//Access the cells in the first worksheet.
Cells cells = workbook.Worksheets[0].Cells;
//Create a range of cells.
Range range = cells.CreateRange(“A6”, “P216”);
//Declare style.
Style stl;
//Create the style adding to the style collection.
stl = workbook.Styles[workbook.Styles.Add()];
//Specify the font settings.
stl.Font.Name = “Arial”;
stl.Font.IsBold = true;
stl.Font.Color = Color.Blue;
//Set the borders
stl.Borders[BorderType.TopBorder].LineStyle = CellBorderType.Thin;
stl.Borders[BorderType.TopBorder].Color = Color.Blue;
stl.Borders[BorderType.LeftBorder].LineStyle = CellBorderType.Thin;
stl.Borders[BorderType.LeftBorder].Color = Color.Blue;
stl.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Thin;
stl.Borders[BorderType.BottomBorder].Color = Color.Blue;
stl.Borders[BorderType.RightBorder].LineStyle = CellBorderType.Thin;
stl.Borders[BorderType.RightBorder].Color = Color.Blue;
//Create StyleFlag object.
StyleFlag flg = new StyleFlag();
//Make the corresponding formatting attributes ON.
flg.Font = true;
flg.Borders = true;
//Apply the style with format settings to the range.
range.ApplyStyle(stl,flg);
//Save the excel file.
workbook.Save(@“d:\test\testrangeborders.xls”);
Hope, this helps a bit.