How to remove borders from cells in a range in Excel using VB.net?

前端 未结 4 1270
眼角桃花
眼角桃花 2021-01-11 13:32

Aim to Achieve: To get rid of borders if any in the cells of range.

I have :

Dim range As Excel.Range = sheet.Range(\"A2:K100\")
For         


        
4条回答
  •  悲&欢浪女
    2021-01-11 14:20

    range.Borders(Excel.XlBordersIndex.xlEdgeLeft).LineStyle = Excel.XlLineStyle.xlLineStyleNone
    range.Borders(Excel.XlBordersIndex.xlEdgeRight).LineStyle = Excel.XlLineStyle.xlLineStyleNone
    range.Borders(Excel.XlBordersIndex.xlEdgeTop).LineStyle = Excel.XlLineStyle.xlLineStyleNone
    range.Borders(Excel.XlBordersIndex.xlEdgeBottom).LineStyle = Excel.XlLineStyle.xlLineStyleNone
    range.Borders(Excel.XlBordersIndex.xlInsideHorizontal).LineStyle = Excel.XlLineStyle.xlLineStyleNone
    range.Borders(Excel.XlBordersIndex.xlInsideVertical).LineStyle = Excel.XlLineStyle.xlLineStyleNone
    

    Removes the borders around the cells and between cells (via xlInsideHorizontal and xlInsideVertical). If you expect diagonal borders, include xlDiagonalDown and xlDiagonalUp.

    Okay, the above code was very verbose. The following should do it too:

    For Each border in range.Borders
        border.LineStyle = Excel.XlLineStyle.xlLineStyleNone
    Next
    

    See: http://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel.borders.aspx

    EDIT:

    While looking over the MSDN page, I'm wondering if this one liner could do it too:

    range.Borders.LineStyle = Excel.XlLineStyle.xlLineStyleNone
    

提交回复
热议问题