Microsoft.Office.Interop.Excel: how to apply a border to ONE CELL

I want to apply a border to a single cell using the Microsoft.Office.Interop.Excel library.

I run a while-loop that searches for an empty cell in a specific column after the cell is found. I want to apply a border to it.

I know that there are many forums on this forum, but I can’t use the range functionality, since I don’t know which cell it applies to exactly.

My idea:

(Excel.Range) xlWS.Cells [row, 1] .Borders.LineStyle = (any value);

Any tips? (besides links to other forums, I've already looked through tons of forms)?

+6
source share
4 answers
Excel.Range range = xlWS.UsedRange; Excel.Range cell = range.Cells[row,column]; Excel.Borders border = cell.Borders; border.LineStyle = Excel.XlLineStyle.xlContinuous; border.Weight = 2d; 

Hope this helps someone! Places a thin line border around the cell [row, column]!

+11
source
  Microsoft.Office.Interop.Excel.Range range = sheet.UsedRange; Microsoft.Office.Interop.Excel.Range cell = range.Cells[1][1]; Microsoft.Office.Interop.Excel.Borders border = cell.Borders; border[XlBordersIndex.xlEdgeLeft].LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous; border[XlBordersIndex.xlEdgeTop].LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous; border[XlBordersIndex.xlEdgeBottom].LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous; border[XlBordersIndex.xlEdgeRight].LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous; 

See more details.

+9
source

Each cell in the range

 Microsoft.Office.Interop.Excel.Range chartRange; chartRange = workSheet.get_Range("a1", "g7"); foreach (Microsoft.Office.Interop.Excel.Range cell in chartRange.Cells) { cell.BorderAround2(); } 

Whole range

 chartRange.BorderAround2(); 
+3
source

This is based on jiverson's answer, but much more concise for basic needs; just create a range, get its borders and add a border to the bottom of the range:

 var platypusRange = _xlSheet.Range[_xlSheet.Cells[1, 1], _xlSheet.Cells[1, 3]]; . . . Borders border = platypusRange.Borders; border[XlBordersIndex.xlEdgeBottom].LineStyle = XlLineStyle.xlContinuous; 

Of course, you can add a border at the top and sides using xlEdgeTop, xlEdgeLeft and xlEdgeRight.

+1
source

Source: https://habr.com/ru/post/949841/


All Articles