无法从Excel中删除工作表,甚至使用VB.NET也没有错误? [英] Unable to delete sheet from excel and it even gives no error using VB.NET?

查看:186
本文介绍了无法从Excel中删除工作表,甚至使用VB.NET也没有错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有:

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!

但工作表并未被删除!

<更新>

< UPDATE >

我也尝试过:

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()

<另一个UPDATE>

< Another UPDATE >

我尝试检查sheet.Delete()布尔值是否成功:

I tried to check sheet.Delete() boolean for success of failure:

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

显示错误:表达式不产生值.在sheet.Delete()中.

It says Error: Expression does not produce a value. in sheet.Delete().

奇怪的是, Microsoft APIreference 说它会产生一个布尔值,但不会,因为它是Sub而不是函数.

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.

怎么回事?

我做错什么了吗?

请帮助我解决此问题!

推荐答案

以下代码对我有用(已编辑,其中包括一些错误检查):

The following code works for me (EDITED to include some error checking):

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

这篇关于无法从Excel中删除工作表,甚至使用VB.NET也没有错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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