【问题标题】:Unable to delete sheet from excel and it even gives no error using VB.NET?无法从 excel 中删除工作表,使用 VB.NET 甚至没有错误?
【发布时间】:2011-07-27 05:14:38
【问题描述】:

我有:

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

我运行了上面的代码,运行成功,绝对没有错误或异常!

但工作表没有被删除!

我也试过了:

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

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

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

它说 Error: Expression does not generate a value. 在 sheet.Delete() 中。

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

发生了什么以及发生了什么?

我做错了吗?

请帮我解决这个问题!

【问题讨论】:

  • getSheetToDelete() 的邮政编码
  • 我已经发布了 getSheetToDelete() 函数。
  • 我还尝试在删除之前选择工作表,这给了我一个例外。不知道为什么会这样!
  • 拥有更多调用函数的代码上下文可能会有所帮助。看我的回答。
  • 您的代码无法编译(例如,Private 对于局部变量声明无效)。请创建一个不起作用的代码的最小工作示例,有关详细信息,请参阅tinyurl.com/so-hints 上的“示例代码”部分。

标签: .net vb.net excel vba


【解决方案1】:

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

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

【讨论】:

  • 我不知道发生了什么魔法......说真的......这真的很奇怪......我只是改为 xlBook = xlApp.Workbooks.Open(file) 而没有单独的 Books 变量..我只是什么都没做……它奏效了……就像魔术一样!这很奇怪,但我很高兴!
【解决方案2】:

我建议您在删除后需要保存工作簿(使用SaveAs)。否则它只会删除内存中的工作表,当您的 Excel 对象超出范围时,它只会关闭工作簿而不保存更改。

Books.SaveAs(...)

【讨论】:

  • 我用过.. WB.Save() WB.Close() 但这并没有帮助.. 它仍然无法正常工作.. 实际上我在原始代码中有这个但我忘了写在这里!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-07-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-08
  • 1970-01-01
相关资源
最近更新 更多