Creating a style for, and setting partial border around a range

I need to set a fine gray border to the left, bottom and right of the below defined range.
I thought this might work.

Style MetricsStyling = Page.CellsFactory.CreateStyle();
MetricsStyling.SetBorder(BorderType.LeftBorder | BorderType.BottomBorder | BorderType.RightBorder, CellBorderType.Hair, Color.LightGray);
Range r = ReportSheet.Cells.CreateRange(7, 9, 4, 5);
r.ApplyStyle(MetricsStyling, new StyleFlag() { Borders = true });

However, the above code throws an exception in the Aspose.Cells:
Object reference not set to an instance of an object.

at Aspose.Cells.Style.SetBorder(BorderType borderEdge, CellBorderType borderStyle, Color borderColor)

The BorderType is a Flags enumeration so one expects you could set it as above. I think you ought to fix it.

Out of curiosity I left only the LeftBorder enum item in the above code and it works but it sets a left border on all the columns inside de range.
How do I accomplish my goal?

Thanks.

@mircea-ion,

You may please give a try to the following sample code and provide your feedback.

// 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.
Aspose.Cells.Range range = cells.CreateRange(7, 9, 4, 5);

// Declare style.
Aspose.Cells.Style stl;

// Create the style adding to the style collection.
stl = workbook.CreateStyle();

// Specify the font settings.
stl.Font.Name = "Arial";
stl.Font.IsBold = true;
stl.Font.Color = Color.Blue;

// Set the borders
stl.Borders[Aspose.Cells.BorderType.LeftBorder].LineStyle = CellBorderType.Hair;
stl.Borders[Aspose.Cells.BorderType.LeftBorder].Color = Color.LightGray;
stl.Borders[Aspose.Cells.BorderType.BottomBorder].LineStyle = CellBorderType.Hair;
stl.Borders[Aspose.Cells.BorderType.BottomBorder].Color = Color.LightGray;
stl.Borders[Aspose.Cells.BorderType.RightBorder].LineStyle = CellBorderType.Hair;
stl.Borders[Aspose.Cells.BorderType.RightBorder].Color = Color.LightGray;


// 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(@"outputSetBorderAroundEachCell.xlsx");

Console.WriteLine("SetBorderAroundEachCell executed successfully.");

@mircea-ion,

And, if you need to apply outline borders to the range, see the sample code for your reference:
e.g
Sample code:

             //Setting outline border for the range.

            Workbook workbook = new Workbook();
           
            Worksheet ReportSheet = workbook.Worksheets[0];
            Range r = ReportSheet.Cells.CreateRange(7, 9, 4, 5);


            //Set outline border.           
            r.SetOutlineBorder(Aspose.Cells.BorderType.BottomBorder, CellBorderType.Hair, Color.LightGray);
            r.SetOutlineBorder(Aspose.Cells.BorderType.LeftBorder, CellBorderType.Hair, Color.LightGray);
            r.SetOutlineBorder(Aspose.Cells.BorderType.RightBorder, CellBorderType.Hair, Color.LightGray);
            
            workbook.Save("e:\\test2\\out1.xlsx"); 

Hope, this helps a bit.

Yes, this is how I did it eventually, based on another post I found in the meantime.
Thanks for confirming this is the recommended way.
It is a pity we can’t combine more than one BorderType in the same call.
I guess I can write an extension I guess.

Anyway, Cheers.

Wouldn’t this set the style of each individual cell (border on each one)? That per documentation would create a Style instance for each cell. Now, my range is not that big in this case but it could be bigger.

@mircea-ion,

The sample code pasted by Ahsan Iqbal will apply borders to each cell in the range while the sample code suggested by me will apply outline border to the range. So you may choose and follow any of the code segments for your needs.

In case you find any issue while applying individual borders (to each cell in the range) or outline borders to the range as we suggested, kindly do paste your sample code (runnable) with details, we will check it soon.