Microsoft.Office.Interop.Excel: How to Apply a border to ONE CELL

青春壹個敷衍的年華 提交于 2019-12-05 05:28:30
            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! Puts a thin line border around cell[row,column]!

jiverson
    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 this answer for more details.

To each cell in 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();
}

To whole range

chartRange.BorderAround2();

This is based on jiverson's answer, but is much more concise for basic needs; simply 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 could add a border to the top and sides, too, using xlEdgeTop, xlEdgeLeft, and xlEdgeRight.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!