我正在尝试将以下代码拆分为多个线程(工作簿中的每个工作表一个线程)。有超过 400,000 行需要共同处理,这在 1 个线程上需要相当长的时间,我希望如果我拆分它,这会加快速度。
自从我使用 Excel Interop 已经有一段时间了,但我很确定下面的代码应该可以工作。我希望它在每个线程上运行:
Dim CurrentRow As String = Nothing
For i As Integer = 1 To sheet.Rows.Count
If Not String.IsNullOrEmpty(sheet.Range("A" & i).ToString) Then
CurrentRow = _
sheet.Range("H" & i).Value.ToString.Trim & "," & _
sheet.Range("A" & i).Value.ToString.Trim & "," & _
sheet.Range("B" & i).Value.ToString.Trim & "," & _
sheet.Range("C" & i).Value.ToString.Trim & "," & _
sheet.Range("D" & i).Value.ToString.Trim & "," & _
sheet.Range("E" & i).Value.ToString.Trim & "," & _
sheet.Range("F" & i).Value.ToString.Trim & "," & _
sheet.Range("G" & i).Value.ToString.Trim & "," & _
sheet.Range("I" & i).Value.ToString.Trim & "," & _
sheet.Range("J" & i).Value.ToString.Trim & "," & _
sheet.Range("K" & i).Value.ToString.Trim & ","
My.Computer.FileSystem.WriteAllText("D:\Lists\" & sheet.Name & ".txt", _
CurrentRow & vbCrLf, True)
ItemCount = ItemCount + 1
ElapsedTime = Now().Subtract(StartTime)
Me.Invoke(Me.AddItemDelegate, New Object() {sheet.Name, ElapsedTime})
Else
ItemCount = 0
Exit For
End If
不幸的是,我在 if 语句中收到以下错误:
System.Runtime.InteropServices.ComException (0x800401A8):Microsoft.Office.Interop.Excel._Worksheet.get_Range (Object Cell1, Object Cell2) 的 HRESULT 0x800401A8 异常
不知道我在这里做错了什么,有人可以帮忙吗?
谢谢!
** 编辑 **
循环似乎有效,但仅通过一次迭代。当它想要进入下一次运行时,它会抛出异常。