Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can a border be set around multiple cells in excel using C#

Tags:

c#

excel

border

I am working on a project that creates excel files.

I am having trouble placing a border on multiple cells to organize the excel file.

Let's say I want a border from cell B5 to B10. There shouldn't be borders between B5, B6, B7,...

Currently, I have this code:

workSheet_range = worksheet.get_Range("B5", "B10");
workSheet_range.Borders.Color = System.Drawing.Color.Black.ToArgb();

It makes the borders, however it places a border around every cell instead of one big border for all cells.

How can I accomplish this?

like image 962
Arnout Avatar asked Jul 31 '12 15:07

Arnout


4 Answers

Maybe this can help :

workSheet_range.BorderAround(Excel.XlLineStyle.xlContinuous, Excel.XlBorderWeight.xlThick);
like image 192
Simon Avatar answered Oct 08 '22 18:10

Simon


I did this without impacting the performance. I am taking a simple excel to format :

Before

enter image description here

I managed to store the range as A1:C4 in a variable dynamically in exRange and used the below code to give border

((Range)excelSheet.get_Range(exRange)).Cells.Borders.LineStyle = XlLineStyle.xlContinuous;


After

enter image description here

like image 44
Sarath KS Avatar answered Sep 20 '22 23:09

Sarath KS


You need to individually set these

.Borders[Excel.XlBordersIndex.xlEdgeBottom] 
.Borders[Excel.XlBordersIndex.xlEdgeRight]
.Borders[Excel.XlBordersIndex.xlEdgeLeft]  
.Borders[Excel.XlBordersIndex.xlEdgeTop]
like image 17
Tim Williams Avatar answered Oct 08 '22 16:10

Tim Williams


This is the code that sets a border around each cell:

xlWS.get_Range("C9", "N9").Cells.Borders.Weight = XL.XlBorderWeight.xlMedium;
like image 4
Santosh Artham Avatar answered Oct 08 '22 16:10

Santosh Artham