Cell and range formatting issue

I format a given range by using a style object as follows:

oSheet.Cells.CreateRange(“A1”, “BJ10”).Style = styleBase

…where styleBase is the style object but has no border properties set.

Then, I try to set the border of a specific cell within that range:

oSheet.Cells(“H5”).Style.Borders(BorderType.BottomBorder).LineStyle = CellBorderType.Thin
oSheet.Cells(“H5”).Style.Borders(BorderType.BottomBorder).Color = Color.Orange


However, when I do this the ENTIRE range of cells is changed, instead of the specific cell.
I’ve attached a graphic of the formatting I need… any help appreciated.

I’m formatting cells using style objects for fonts and alignments etc. I’m then trying to set border formats for specific cells and ranges after that.

When you use Range.Style to set styleBase to this range, all cells in this range contain the same style object. So if you change one of them, all formattings are changed.

You can change your code to:

oSheet.Cells.CreateRange(“A1”, “BJ10”).Style = styleBase


Dim newStyle as Style = excel.Styles(excel.Styles.Add())
newStyle.Copy(styleBase)
newStyle.Borders(BorderType.BottomBorder).LineStyle = CellBorderType.Thin
newStyle.Borders(BorderType.BottomBorder).Color = Color.Red

oSheet.Cells(“H5”).Style = newStyle

THANKS!!

This seems to have worked a treat! Big Smile

@mecED,
Aspose.Excel is discontinued and no more available now. It is replaced by Aspose.Cells that is advanced and contains all the features supported by different versions of MS Excel. Using this new product you can set the style for a range and set different style to a cell within the range as demonstrated in the following sample code:

Workbook workbook = new Workbook();
Worksheet sheet = workbook.Worksheets[0];
sheet.Cells.ImportObjectArray(Enumerable.Range(1, 6).Cast<object>().ToArray(),
    1, //this table is start at row 
    1, false);
Style style = workbook.CreateStyle();
style.Name = "SolidStyle";

// Set the Style fill color to Yellow
style.Pattern = BackgroundType.Solid;
style.ForegroundColor = Color.Yellow;
//style.BackgroundColor = Color.Red;

       
var headerRange = sheet.Cells.CreateRange(1, 1, 1, 6);
            
headerRange.ApplyStyle(style, new StyleFlag { All = true });

Cell cell = sheet.Cells["B2"];
Style style2 = cell.GetStyle();

style2.Font.Name = "Arial";
style2.Font.IsBold = true;
style2.Font.IsItalic = true;
style2.Font.Color = Color.Red;

// Setting the line style of the top border
style2.Borders[BorderType.TopBorder].LineStyle = CellBorderType.Thick;

// Setting the color of the top border
style2.Borders[BorderType.TopBorder].Color = Color.Orange;

cell.SetStyle(style2);
workbook.Save("output.xlsx");

Refer to the following article for more details about applying styles:
Applying Styles on Cells

Download the free trial version here for testing the features of this new product.
Aspose.Cells for .NET (Latest Version)

A detailed solution is available here which can be used for testing different features of this product.