We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Aspose.Cells Style

I tried to apply a style

Excel.Range RangeTitle = _worksheet.Cells.CreateRange(“A2”, “K2”);
RangeTitle.Value = “Bg”;
Excel.Style TitleStyle = workbook.Styles[0];
TitleStyle.Font.Size = 20;
TitleStyle.Font.IsBold = true;
TitleStyle.Font.Color = ColorTranslator.FromHtml("#E9AF32");
TitleStyle.BackgroundColor = ColorTranslator.FromHtml("#E9AF32");
TitleStyle.HorizontalAlignment = Excel.TextAlignmentType.Center;

1) I applied font color to A2 to K2, but it is applying for all A2 to K250.
2) Background color is not working.

Excel.Range RangeComplete = _worksheet.Cells.CreateRange(“A4”, “K250”);
RangeComplete.SetOutlineBorder(Excel.BorderType.RightBorder, Excel.CellBorderType.Medium, Color.FromArgb(0, 0, 128));
Excel.Style DataStyle = workbook.Styles[1];
DataStyle.Font.Color = ColorTranslator.FromHtml("#000000");

By using ImportDataTable i got date format, but after applying Datastyle to the rangecomplete am getting date in % form.
Eg: Am getting this 11/20/2013 2:48:15 AM in 41598%.

4) How to apply borders (not outline borders) for the range.


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:
Sample code:

Workbook workbook = new Workbook();
Worksheet _worksheet = workbook.Worksheets[0];
Range RangeTitle = _worksheet.Cells.CreateRange(“A2”, “K2”);
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);


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:
//Set the shading/ fill color for the cells in the range.
TitleStyle.ForegroundColor = ColorTranslator.FromHtml("#E9AF32");
TitleStyle.Pattern = BackgroundType.Solid;

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.

4) Please see the sample code below for your reference:
//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.

//Save the excel file.

Hope, this helps a bit.