vb.net Vb.net按索引删除excel行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31474255/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 19:18:15  来源:igfitidea点击:

Vb.net delete excel rows by index

vb.netexcel

提问by gandalf

I'm trying to delete rows by index using this code:

我正在尝试使用以下代码按索引删除行:

Dim xlApp As New Excel.Application
Dim xlWorkBook As Excel.Workbook
Dim xlWorkSheet As Excel.Worksheet
Try
    xlWorkBook = xlApp.Workbooks.Open(TempRefPath)
    xlWorkSheet = xlWorkBook.Worksheets("w1")

    Dim empty As Integer
    For empty = i + 2 To 2000
        xlWorkSheet.DeleteRow
    Next
    xlWorkBook.Save()
Catch ex As Exception
    MessageBox2(ex.Message, Me.Page)
Finally
    If Not IsNothing(xlWorkBook) Then
        xlWorkBook.Close()
    End If
    xlApp.Quit()
End Try

but the function delete is not found.

但是没有找到删除函数。

回答by Alex K.

There is no DeleteRowand if there were how would it know what row to delete? You can do this without a loop, to delete rows 5=>10:

没有DeleteRow,如果有它怎么知道要删除哪一行?您可以在没有循环的情况下执行此操作,以删除行 5=>10:

xlWorkSheet.Rows(5 & ":" & 10).Delete()

回答by santosh

Dim xlRange1 As Excel.Range = Nothing
xlRange1 = CType(xlWorkSheet.Rows(RowIndex), Excel.Range)
xlRange1.Delete()

from here.

这里

回答by Borrell Fankam

I've found that the following also works really well!

我发现以下内容也非常有效!

xlC1Sheet.Rows("10:19").delete()

Where xlC1Sheet is the sheet you're currently using.

其中 xlC1Sheet 是您当前使用的工作表。

This will delete all rows from row 10, up to and including row 19

这将删除从第 10 行到第 19 行的所有行