简体   繁体   English

无法从Excel删除工作表,甚至使用VB.NET没有错误?

[英]Unable to delete sheet from excel and it even gives no error using VB.NET?

I have: 我有:

Public Class ExcelProcess
    Private App As New Excel.Application
    Private Books As Excel.Workbooks = App.Workbooks
    Private WB As Excel.Workbook

Public Sub deleteSheets()
    Dim sheet As Excel.Worksheet = getSheetToDelete()
    sheet.Activate()
    sheet.Delete()
    WB.Save()
    WB.Close()
End Sub

Private Function getSheetToDelete() As Excel.Worksheet
     Try
        WB = Books.Open("file.xlsx")
    Catch ex As Exception
        InputOutput.print(ex.Message)
        End
    End Try
    Return WB.Sheets(1)
End Function

End Class

I ran the above code, and it ran successfully with absolutely no errors or exceptions! 我运行上面的代码, 它成功运行,绝对没有错误或异常!

But the sheet didn't get deleted! 但是这张表没有被删除!

< UPDATE > <更新>

I have also tried: 我也尝试过:

sheet.Select()
sheet.Delete()

' Now, this gave me an exception:
Unhandled Exception: System.Runtime.InteropServices.COMException: Exception from HRESULT: 0x800A03EC
at Microsoft.Office.Interop.Excel._Worksheet.Select(Object Replace)
at DealerCapReport.ExcelProcess.deleteSheets()
at DealerCapReport.MainModule.Main()

< Another UPDATE > <另一个更新>

I tried to check sheet.Delete() boolean for success of failure: 我试图检查sheet.Delete()boolean是否成功失败:

If sheet.Delete() Then        ' Error: Expression does not produce a value.
    Console.WriteLine("Can-not Delete")
End If

It says Error: Expression does not produce a value. 它表示错误:表达式不会产生值。 in sheet.Delete(). 在sheet.Delete()中。

The strange thing is that the Microsoft API reference says that it would produce a Boolean, but it doesn't as it is a Sub and not a function. 奇怪的是, Microsoft API引用说它会生成一个布尔值,但它不是因为它是Sub而不是函数。

How and what is happening? 怎么样,发生了什么?

Am I doing something wrong? 难道我做错了什么?

Please help me fix this! 请帮我解决这个问题!

The following code works for me ( EDITED to include some error checking ): 以下代码适用于我( EDITED包括一些错误检查 ):

Public Class ExcelProcess
    Private xlApp As Excel.Application
    Private xlBook As Excel.Workbook
    Private xlSheet As Excel.Worksheet

    Public Sub New(ByVal file As String)
        xlApp = New Excel.Application
        'xlApp.Visible = True 'for debugging
        xlApp.DisplayAlerts = False 'prevent user dialogs
        xlBook = xlApp.Workbooks.Open(file)
    End Sub

    Public Sub Quit()
        If Not IsNothing(xlBook) Then
            xlBook.Save()
            xlBook.Close()
        End If
        If Not IsNothing(xlApp) Then xlApp.Quit()
    End Sub

    Public Sub deleteSheet(ByVal Index As Integer)
        If Index > xlBook.Worksheets.Count Then
            Throw New Exception("You cannot delete a worksheet that does not exist")
            Exit Sub
        End If
        If xlBook.Worksheets.Count = 1 Then
            Throw New Exception("You cannnot delete the only worksheet in a workbook")
            Exit Sub
        End If
        xlSheet = CType(xlBook.Worksheets(Index), Excel.Worksheet)
        xlSheet.Activate()
        xlSheet.Delete()
    End Sub

End Class

I would suggest that you need to save your workbook after you do the delete (using SaveAs ). 我建议您在执行删除后使用SaveAs保存工作簿。 Otherwise it will just delete the in-memory worksheet and when your Excel object goes out of scope it will just close the workbook without saving the changes. 否则它只会删除内存中的工作表,当您的Excel对象超出范围时,它将关闭工作簿而不保存更改。

Books.SaveAs(...)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM