在 Asp.net 中通过 Excel 互操作使用以下代码处理多线程时:
Public Class ImportService
Public Function ImportsFiles(ByVal files as list(Of String))
Dim rowEffect As Integer = 0
For j as Integer=0 to files.Count-1
dim fileAddress=files(j)
Dim app As New Application
Dim Wbook As Workbook
Try
Wbook = app.Workbooks.Open(fileAddress, [ReadOnly]:=True)
For i As Integer = 1 To Wbook.Sheets.Count
If Not Wbook.Sheets(i).Name.ToString.ToLower.Contains("partial read of load profile") Then
Dim con As New System.Data.OleDb.OleDbConnection("provider=Microsoft.ACE.OLEDB.12.0; Data Source=" & fileAddress & "; Extended Properties=""Excel 12.0 Xml;HDR=No;IMEX=1"";")
Dim cmd As New OleDbCommand
Dim s As String = "SELECT * FROM [" & Wbook.Sheets(i).Name & "$]"
cmd.CommandText = s
cmd.Connection = con
Dim da As New OleDbDataAdapter
da.SelectCommand = cmd
Dim dt As New System.Data.DataTable
da.Fill(dt)
Dim err As New Dictionary(Of String, String)
Dim fileDate As String = Wbook.Sheets(i).Name.ToString.Trim.Replace(" ", "/")
rowEffect += InsertLps(Path.GetFileName(fileAddress), fileDate, dt.Rows, err)
dt.Dispose()
da.Dispose()
con.Dispose()
cmd.Dispose()
End If
ReleaseCOMObject(Wbook.Sheets(i))
Next
Wbook.Close(SaveChanges:=False)
Catch ex As Exception
summary.Add(fileAddress, ex.Message)
Finally
GC.Collect()
GC.WaitForPendingFinalizers()
GC.Collect()
GC.WaitForPendingFinalizers()
If app.Workbooks IsNot Nothing Then
For Each wb In app.Workbooks
For Each ws In wb.Worksheets
ReleaseCOMObject(ws)
Next
wb.Close(False)
ReleaseCOMObject(wb)
Next
End If
app.Workbooks.Close()
app.Quit()
ReleaseCOMObject(app)
GC.WaitForFullGCComplete()
Next
End Function
End Class
发生错误:
为此 RuntimeCallableWrapper 转换到 COM 上下文 0x946d20 失败,并出现以下错误:调用的对象已与其客户端断开连接。(来自 HRESULT 的异常:0x80010108 (RPC_E_DISCONNECTED))。这通常是因为创建此 RuntimeCallableWrapper 的 COM 上下文 0x946d20 已断开连接,或者它正忙于做其他事情。从当前 COM 上下文(COM 上下文 0x946c68)释放接口。这可能会导致损坏或数据丢失。为避免此问题,请确保所有 COM 上下文/单元/线程保持活动状态并且可用于上下文转换,直到应用程序完全使用代表存在于其中的 COM 组件的 RuntimeCallableWrapper 完成。
我正在尝试使用 Quartz.net 和更多触发器在后台工作。当 2 触发器通过此代码开始工作时:
Public Class ImportJob
Implements IJob
Public Sub Execute(context As IJobExecutionContext) Implements IJob.Execute
Dim importHelpers As New ImportFilesHelpers
Dim exts As New List(Of String)
exts.Add(".xls")
Dim files = importHelpers.GetFiles(ImportFilesHelpers.BaseAssress, exts)
Dim import = New ImportService()
import.ImportsFiles(files)
End Sub
End Class
此行中的以下错误 Acoures
Dim fileDate As String = Wbook.Sheets(i).Name.ToString.Trim.Replace(" ", "/")