Excel在预列list.Contains()时抛出HRESULT: 0x800AC472异常 &g



我正在VS2019上编写一个Windows窗体应用程序,该应用程序将打开excel文件并执行嵌套循环。

基本上,有两个整数列表。代码将遍历每一行,检查单元格A的值是否存在于任何列表中,然后将字符串赋值给单元格b。

原始代码中的列表大约有400个元素,但我将其缩减到这里。然后有趣的事情发生了。当我运行带有400+元素的原始代码时,它循环行并中途停止,然后它给了我来自HRESULT: 0x800ac472的"异常"。然而,当我运行下面的代码时,它没有错误。

所以我想如果这个庞大的列表是问题所在。我想知道你们中是否有人遇到过这种情况,以及你是如何解决的。

(更新)我向列表中添加了更多元素,并尝试运行它。它确实抛出了同样的异常。我还尝试减少元素的数量,并再次运行,它工作没有错误。所以我猜这是因为元素的数量。

Private Sub generateZone(ByVal worksheet As Object)
'Initialise Excel Object
xlsApp = New Excel.Application
'Open file
xlsWorkBook = xlsApp.Workbooks.Open("File location")
'Open worksheet(according to the spreadsheet)
xlsWorkSheet = xlsWorkBook.Worksheets("sheet1")
'Excel interaction setting
With xlsApp
.Visible = True
.Application.Visible = True
.DisplayAlerts = False
.EnableEvents = False
End With        
Dim emZone1 As New List(Of Integer)(New Integer() {87371, 87390, 94614, 92000, 82898, 96500, 99124, 93260, 82496, 97858, 90323, 88083, 80770, 84186, 86318, 91922, 85987, 80635, 84079, 96691, 85578, 83108, 96081, 87642, 96703, 96692, 99193, 93039, 97003, 89374, 99252, 82305, 87907, 90966, 80517, 88471, 92395, 86109, 87112, 92849, 93853, 91136, 90512, 97143, 96105, 93966, 81136, 97218, 97816, 82525, 97714, 98175, 94940, 97262, 81750, 92075, 98905, 96199, 94072, 83841, 88243, 98375, 84142, 92818, 83527, 97446, 88632, 86542, 84768, 86283, 84910, 88986, 92802, 99145, 81487, 84729, 80010, 90896, 99418, 87545, 95937, 89904, 88073, 85255, 87285, 88442, 86325, 90223, 92048, 85160, 98768, 80283, 91273, 92077, 91043, 81409, 96042, 82536, 92726, 91980})

Dim emZone2 As New List(Of Integer)(New Integer() {86634, 92330, 95970, 95577, 87510, 89481, 94248, 93860, 81857, 82810, 93228, 80095, 94437, 84887, 88766, 92706, 92264, 88109, 91992, 82751, 94767, 95397, 96066, 91667, 94059, 89419, 82796, 82310, 86961, 85681, 93969, 81736, 81009, 97445, 80741, 92154, 84923, 86182, 91660, 90665, 81388, 87722, 94031, 94678, 84074, 80550, 82953, 81317, 95132, 92163})

'get last row
Dim lastRow As Integer
lastRow = worksheet.UsedRange.Rows.Count

'loop through every row
For i As Integer = 1 To lastRow
'get pronvice from column J
Dim province As String = worksheet.Range("J" & i).Value
If province = "Sabah" Or province = "Sarawak" Then
'check zone1
For Each zone1 As Integer In emZone1
If emZone1.Contains(worksheet.Range("L" & i).Value) Then
worksheet.Range("M" & i).Value = "Zone 1"
Else
'check zone2
For Each zone2 As Integer In emZone2
If emZone2.Contains(worksheet.Range("L" & i).Value) Then
worksheet.Range("M" & i).Value = "Zone 2"
' Exception from HRESULT: 0x800AC472 shows here
Else
'if not in both zone
worksheet.Range("M" & i).Value = "EM"
End If
Next
End If
Next
Else
'if not sabah and sarawak
worksheet.Range("M" & i).Value = "WM"
End If
Next
'releaseObject(xlsWorkSheet)
'releaseObject(xlsWorkBook)
'releaseObject(xlsApp)
End Sub

不确定这是否相同,但另一个SO帖子与此相同的错误已通过以下答案解决:

"在我的情况下,抛出异常是因为我的excel互操作工具显示了一个模态对话框(与过期的许可证密钥相关-我感到羞耻)。如果我关闭对话框(在后台显示),然后在Visual Studio中点击"继续",程序就能够连接到xlsx文件并成功检索数据。-mbmihura

你确认没有弹出窗口或任何东西出现吗?

你的excel也有授权吗?

如果不是this,

我会给你的循环添加一个限制,并试着找出在错误之前是否有固定的迭代次数,如果是,那么就有一些默认的限制被施加。

最后一个想法,

我在很多例子中看到这些互操作excel调用没有得到适当的处理,这导致许多excel在后台卡住运行。

检查你的任务管理器,确保没有发生这种情况。如果是,请关闭它们并再次尝试测试。

我认为您只需要在读取值之前检查空单元格。当单元格为空时,Excel将抛出异常,您尝试将其解析为字符串

Dim xlsRange As Excel.Range = Nothing
' In your loop
xlsRange = DirectCast(xlsWorkSheet.Cells("J" & i), Excel.Range)
If xlsRange.Value IsNot Nothing Then
Dim province = xlsRange.Value.ToString()
End If
' /Loop
' always release it at the end
If xlsRange IsNot Nothing Then Marshal.ReleaseComObject(xlRange)

我仍然喜欢将所有内容加载到对象中,并在释放所有Excel对象后对其进行操作

最新更新