How to add borders to range of cells in an Excel worksheet using Aspose.Cells for .NET

Hello, maybe I am missing something

the example at Add Borders to Cells in a Worksheet|Documentation
shows how to set the border cells using a range of 1 cell size, but what about a range that is 100 rows by 100 columns, and I want to have all the cells inside it have borders, not just the outside perimeter.

thanks in advance for all your help

Hi Leon,


Thank you for contacting Aspose support.

Please try the following piece of code that creates a style with 4 borders and applies it to a range of cells where each cell is styled individually. Please also have a look at the article on adding cell borders.

C#

Workbook workbook = new Workbook();
Range range = workbook.Worksheets[0].Cells.CreateRange(“A1:G10”);
Style style = workbook.CreateStyle();
style.Borders[BorderType.LeftBorder].LineStyle = CellBorderType.Thick;
style.Borders[BorderType.RightBorder].LineStyle = CellBorderType.Thick;
style.Borders[BorderType.TopBorder].LineStyle = CellBorderType.Thick;
style.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Thick;
range.ApplyStyle(style, new StyleFlag() { Borders = true});
workbook.Save(“D:/output.xlsx”, SaveFormat.Xlsx);

Hi BAbar,
I have this exact code, and what id does is only fill in the perimeter of the range, not the individual cells inside it.

Thanks

Hi Leon,


Have you tested the exact code or have you modified it? If you have modified it then please share your updated code here along with it’s resultant spreadsheet.

I have tested the code again while using the latest version of Aspose.Cells for .NET 8.4.2.2 and the result is up to the expectation. The code is actually applying the borders to all individual cells in a given range, Please check the attached snapshot & resultant spreadsheet as attached. If you have different requirement then please share your desired results that you may create manually using Excel application.

Hi Babar, thank you, your code worked, and your mention that I may have made modifications led me to review all the code, and it was not the same per say, you are using a workbook.CreateStyle(), where as i was doing new Style();

The issue is resolved, thank you for your quick responses as always.

Cheers!

Hi Leon,


It is good to know that you are up & running again. Please note, it is advised to use the Workbook.CreateStyle method instead of Style constructor because it initializes the style object and adds it to the pool of styles.

Please feel free to contact us back in case you need our further assistance with Aspose APIs.