To add a border to one or more sides of the Excel range (a range of cells, which can usually consist of 1..many rows and 1..many columns, but for this particular scenario we probably want to stick with one row and 1..many columns), you only need to do three things:
0) Define the range 1) Get a reference to the Range Borders array 2) Assign a border to one or more of the Border array edges/sides (top, bottom, left, right)
First, define the range that you want to work on like this:
var rowToBottomBorderizeRange = _xlSheet.Range[_xlSheet.Cells[rowToBottomBorderize, ITEMDESC_COL], _xlSheet.Cells[rowToBottomBorderize, TOTALS_COL]];
Next, refer to the Range Borders array as follows:
Borders border = rowToBottomBorderizeRange.Borders;
Finally, assign a border to one or more edges of the Border array; for example, if you want to add boder to the bottom, for example:
border[XlBordersIndex.xlEdgeBottom].LineStyle = XlLineStyle.xlContinuous;
Putting it all together, the code could be:
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 can make a way out of it:
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 above example only shows adding a bottom border, but you can easily add top, left or right border lines by replacing "xlEdgeBottom" with "xlEdgeTop", "xlEdgeRight" or "xlEdgeLeft"
Or you can add borders around the range, like this:
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 as follows:
private Worksheet _xlSheet;
... and refer to the Microsoft.Office.Interop.Excel assembly in your solution.