【问题标题】:How to remove borders from cells in a range in Excel using VB.net?如何使用VB.net从Excel范围内的单元格中删除边框?
【发布时间】:2011-08-07 18:51:54
【问题描述】:

目标实现: 如果范围的单元格中有边框,则去除边框。

我有:

Dim range As Excel.Range = sheet.Range("A2:K100")
For Each cell In range
    // Some cells in the Range has borders
    // How to remove borders from cells in the range
Next cell

请帮忙..!

我是 Vb.net 的新手!

【问题讨论】:

    标签: .net vb.net excel border vba


    【解决方案1】:
    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
    

    删除单元格周围和单元格之间的边框(通过xlInsideHorizontalxlInsideVertical)。如果您希望使用对角线边框,请包括 xlDiagonalDownxlDiagonalUp

    好的,上面的代码非常冗长。以下也应该这样做:

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

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

    编辑:

    在查看 MSDN 页面时,我想知道这个班轮是否也可以做到:

    range.Borders.LineStyle = Excel.XlLineStyle.xlLineStyleNone
    

    【讨论】:

    • 如果范围..太大的话会很慢..那么,它可以一次为整个范围做吗?
    • 它应该非常快,因为您不会像样本中那样遍历每个单元格。你试过了吗?
    • 我想它就像我们在每个单元格的边界上进行迭代一样。我的意思是你的解决方案看起来不错,但是没有针对整个范围的直接方法吗?我想一定有一个?
    • 等等.. 一个班轮.. 就是我要找的.. 太棒了!!谢谢!!
    • 单行在 Windows 7 上的 Excel 2007 中为我工作 :)
    【解决方案2】:

    Range("A2:K100").Borders.LineStyle = xlNone

    【讨论】:

    • 这是正确答案。其他的则不必要地复杂。
    • 优秀的答案。
    【解决方案3】:

    为什么所有的答案都那么复杂?

    对于整个工作表使用...

    With .Cells
           .Borders.LineStyle = xlLineStyleNone
    End With
    

    对于一个范围,只需根据需要替换 .Cells

    【讨论】:

    • 2020 这就是我正在寻找的答案。谢谢。
    • 或简单地Cells.Borders.LineStyle=xlLineStyleNone 清除活动工作表上的所有边框。
    【解决方案4】:

    检查NamedRange.BorderAround Method

    Dim range As Excel.Range = sheet.Range("A2:K100")
    range.BorderAround(Excel.XlLineStyle.xlLineStyleNone, Excel.XlBorderWeight.xlThin, Microsoft.Office.Interop.Excel.XlColorIndex.xlColorIndexAutomatic, missing)
    

    干杯,祝你好运!

    【讨论】:

    • 它管理范围周围的边界,而不是范围内的边界。我正在修改问题以明确。
    猜你喜欢
    • 1970-01-01
    • 2017-05-25
    • 1970-01-01
    • 2022-01-24
    • 1970-01-01
    • 2018-08-09
    • 1970-01-01
    • 2016-05-20
    相关资源
    最近更新 更多