错误:删除行,无法使用excel和VB.NET打开.xlsx文件 [英] Error: deleting row and cannot open file .xlsx using excel and VB.NET

查看:194
本文介绍了错误:删除行,无法使用excel和VB.NET打开.xlsx文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

伙计帮我修好这段代码,提前谢谢

让我头疼的找出来但是找不到简单的方法

我想删除那里的行我可以选择datagridview中的单元格以在Excel 2010中删除但始终显示错误。再次感谢。



我尝试了什么:



Guys Help me to fix this code thanks in advance
Its make me headache to find out but cant to find to easy way
I want to delete the row where can i select the cell in datagridview to delete in Excel 2010 but always show the error. thanks again.

What I have tried:

    Dim ExcelApp As Excel.Application
    Dim ExcelWorkBk As Excel.Workbook
    Dim ExcelWorkSht As Excel.Worksheet
Public Sub ExcelRemoveRow(ByVal FileName As String, ByVal SheetName As String, ByVal RowIndex As Int32)
        ExcelApp = New Excel.Application
        Try
            'This code are another form and this is my sample code
            'Filename = GetAppPath() & "\backup\cbms.xlsx"
            'ExcelRemoveRow(Filename, "enumeratorrecord", DataGridView1.CurrentRow.Cells(0).Value)

            ExcelWorkBk = ExcelApp.Workbooks.Open(FileName) 'error code here

            '--------------------------ERROR-------------------------------------
            'Excel cannot open the file 'cbms.xlsx' because the file format 
            'or file extension is not valid. Verify that the file has not been 
            'corrupted and that the file extension matches the format of the file.
            ExcelWorkSht = ExcelWorkBk.Worksheets("enumeratorrecord")

            'Dim empty As Integer
            'For empty = i + 1 To 2000
            ExcelWorkSht.Rows(RowIndex).Delete()
            'Next
            ExcelWorkBk.Save()
            grideview()
            MessageBox.Show("Record Deleted Successfully", "Process Complete", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        Finally
            If Not IsNothing(ExcelWorkBk) Then
                ExcelWorkBk.Close()
            End If
            ExcelApp.Quit()
        End Try
    End Sub

推荐答案

您忘了在此行中传递 Shift 参数:

You forgot to pass Shift argument in this line:
ExcelWorkSht.Rows(RowIndex).Delete(_Here_)



请参阅: Range.Delete(Object)方法(Microsoft.Office.Interop.Excel)| Microsoft Docs [ ^ ]


这篇关于错误:删除行,无法使用excel和VB.NET打开.xlsx文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆