Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

How to Add a Border to an Excel Range

0.00/5 (No votes)
12 Nov 2015CPOL1 min read 18.9K  
Quick-and-Clean tip on adding a border to the specified edge[s] of an Excel range of cells

Easy as 1-2-3

To add a border to one or more sides of an Excel Range (range of cells, which can normally be comprised of 1..many rows and 1..many columns, but for this specific scenario, we probably want to stick with one row and 1..many columns), you only need do three things:

  1. Define the range
  2. Get a reference to the Range's Borders array
  3. Assign a border to one or more of the Border array's edges/sides (top, bottom, left, right)

First, define the range over which you want to operate on like so:

C#
var rowToBottomBorderizeRange = _xlSheet.Range[_xlSheet.Cells[rowToBottomBorderize, ITEMDESC_COL], 
	_xlSheet.Cells[rowToBottomBorderize, TOTALS_COL]];

Next, obtain a reference to the Range's Borders array like this:

C#
Borders border = rowToBottomBorderizeRange.Borders;

Finally, assign a border to one or more of the Border array's edges; for example, if you want to add a border to the bottom, like so:

C#
border[XlBordersIndex.xlEdgeBottom].LineStyle = XlLineStyle.xlContinuous;

Putting it all together, the code could be:

C#
var rowToBottomBorderizeRange = _xlSheet.Range[_xlSheet.Cells[rowToBottomBorderize, 
	ITEMDESC_COL], _xlSheet.Cells[rowToBottomBorderize, TOTALS_COL]];
Borders border = rowToBottomBorderizeRange.Borders;
border[XlBordersIndex.xlEdgeBottom].LineStyle = XlLineStyle.xlContinuous;

If you do this in several places, you could make a method out of it:

C#
private void AddBottomBorder(int rowToBottomBorderize)
{
    var rowToBottomBorderizeRange = _xlSheet.Range[_xlSheet.Cells[rowToBottomBorderize, ITEMDESC_COL], 
		_xlSheet.Cells[rowToBottomBorderize, TOTALS_COL]];
    Borders border = rowToBottomBorderizeRange.Borders;
    border[XlBordersIndex.xlEdgeBottom].LineStyle = XlLineStyle.xlContinuous;
}

The example above shows just adding a bottom border, but you can add top, left, or right border lines just as easily, replacing "xlEdgeBottom" with "xlEdgeTop", "xlEdgeRight", or "xlEdgeLeft"

Or, you could add borders all around a range like this:

C#
private void Add360Borders(int rowToBorderize)
{
    var rowToBottomBorderizeRange = _xlSheet.Range[_xlSheet.Cells[rowToBorderize, ITEMDESC_COL], 
		_xlSheet.Cells[rowToBorderize, TOTALS_COL]];
    Borders border = rowToBorderizeRange.Borders;
    border[XlBordersIndex.xlEdgeBottom].LineStyle = XlLineStyle.xlContinuous;
    border[XlBordersIndex.xlEdgeTop].LineStyle = XlLineStyle.xlContinuous;
    border[XlBordersIndex.xlEdgeLeft].LineStyle = XlLineStyle.xlContinuous;
    border[XlBordersIndex.xlEdgeRight].LineStyle = XlLineStyle.xlContinuous;
}

Note: You will need to define the sheet like this:

C#
private Worksheet _xlSheet;

...and reference the Microsoft.Office.Interop.Excel assembly in your solution.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)