Border around a range

Hi,


Hope you can help me, i want a border around a range of cells but what i get is that every cell in that range has a border. For example i want a border around “A1:A4”, my code:

Aspose.Cells.Style style = new Style();
style.Borders[BorderType.TopBorder].Color = System.Drawing.Color.Red;
style.Borders[BorderType.TopBorder].LineStyle = CellBorderType.Thick;
style.Borders[BorderType.BottomBorder].Color = System.Drawing.Color.Red;
style.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Thick;
style.Borders[BorderType.LeftBorder].Color = System.Drawing.Color.Red;
style.Borders[BorderType.LeftBorder].LineStyle = CellBorderType.Thick;
style.Borders[BorderType.RightBorder].Color = System.Drawing.Color.Red;
style.Borders[BorderType.RightBorder].LineStyle = CellBorderType.Thick;

Range rb = ws.Cells.CreateRange(“A1:A4”);
rb.SetStyle(style);

I also tried:
rb.ApplyStyle(style, new Aspose.Cells.StyleFlag() { All = true,Borders = true });

Thanks in advance,

Willem-Jan

Hi Willem-Jan,

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.

C#


Workbook workbook = new Workbook();
Range rb = workbook.Worksheets[0].Cells.CreateRange(“B1:B4”);
rb.SetOutlineBorders(CellBorderType.Medium, Color.Red);
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 ?

@Akash007

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.

Download Link:
Input and Output Excel files and Screenshot.zip (60.5 KB)

C#

// Create the workbook.
Workbook wb = new Workbook("Test.xlsx");

// Access first worksheet.
Worksheet ws = wb.Worksheets[0];

// 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");

Screenshot:

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.

@Akash007,

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.