Thank you for using Aspose products.
Please use Range.SetOutlineBorders method for your requirement. I have included the sample code snippet and my output with this post.
Workbook workbook = new Workbook();
Range rb = workbook.Worksheets.Cells.CreateRange(“B1:B4”);
workbook.Save(myDir + “output.xlsx”);
Hope this helps.
Thanks that works!
Hi, Can we apply styles over a range and also keep the borders which were already applied around the cells in that range from some previous operation ?
Thanks for using Aspose APIs.
Yes, it is possible with Range.ApplyStyle() method. Please see the following sample code, its input and output Excel files and screenshot for your reference.
As you can see inside the screenshot, we have applied horizontal formatting and set the font bold and font color as blue while keeping the borders intact.
Input and Output Excel files and Screenshot.zip (60.5 KB)
// Create the workbook. Workbook wb = new Workbook("Test.xlsx"); // Access first worksheet. Worksheet ws = wb.Worksheets; // Create the cell range. Range rng = ws.Cells.CreateRange("D3:J13"); // Create a style for range. Style st = wb.CreateStyle(); st.HorizontalAlignment = TextAlignmentType.Center; st.Font.Color = Color.Blue; st.Font.IsBold = true; // Stlye flag will tell what properties of Style object needs to applied. StyleFlag flag = new StyleFlag(); flag.Borders = false; flag.FontColor = true; flag.FontBold = true; flag.HorizontalAlignment = true; // Apply the style to range. rng.ApplyStyle(st, flag); // Save the workbook to xlsx format. wb.Save("output.xlsx");
Hi, Thanks for the previous update. It helped a lot. I have another query. While applying a style over a range, is there a way to only apply the properties that have been set explicitly in the style object? I know that for an individual cell you have the method ‘setStyle’ with the ‘explicitFlag’ that helps to achieve the same. I am looking for something similar at range level.
You may try to use Range.SetStyle() method if it helps. Alternatively you may loop through the range of cells and use Cell.SetStyle method (overload) to apply style explicitly to individual cells in the range/area.