我有兩個後臺工作人員,需要將事務從第一個傳遞給另一個工作人員。在後臺工作人員之間傳遞Excel變量
這就是我試圖通過:
Dim APP As Excel.Application = New Microsoft.Office.Interop.Excel.Application()
Dim workbook As Excel.Workbook
Dim worksheet As Excel.Worksheet
Dim worksheetRange As Excel.Range
Dim status As String
這是如何,我想通過它:
Private Sub bwExport_DoWork(sender As Object, e As DoWorkEventArgs) Handles bwExport.DoWork
Dim APP As Excel.Application = New Microsoft.Office.Interop.Excel.Application()
Dim workbook As Excel.Workbook
Dim worksheet As Excel.Worksheet
Dim worksheetRange As Excel.Range
Dim status As String
'code stuff here
Dim ExportList As New List(Of Object)(5)
ExportList.Add(APP)
ExportList.Add(workbook)
ExportList.Add(worksheet)
ExportList.Add(worksheetRange)
ExportList.Add(status)
e.Result = ExportList
End Sub
Private Sub bwExport_RunWorkerCompleted(sender As Object, e As RunWorkerCompletedEventArgs) Handles bwExport.RunWorkerCompleted
bwExportFinish.RunWorkerAsync(e.Result)
End Sub
而這就是我試圖找回它的方式:
Private Sub bwExportFinish_DoWork(sender As Object, e As DoWorkEventArgs) Handles bwExportFinish.DoWork
Dim ExportList As List(Of Object) = TryCast(e.Result, List(Of Object))
Dim APP As Excel.Application = TryCast(ExportList(0), Excel.Application)
Dim workbook As Excel.Workbook = TryCast(ExportList(1), Excel.Workbook)
Dim worksheet As Excel.Worksheet = TryCast(ExportList(2), Excel.Worksheet)
Dim worksheetRange As String = TryCast(ExportList(3), String)
Dim status As String = TryCast(ExportList(4), String)
'Save spreadsheet
Dim SavePath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) & "\" & label1.Text & " " & Status & " Status " & System.DateTime.Now.ToString("MM.dd.yyyy HH;mm;ss") & ".xlsx"
workbook.SaveAs(SavePath)
workbook.Close(False)
End Sub
這是我發現了錯誤:
1.線:昏暗APP作爲Excel.Application = TryCast(ExportList(0),Excel.Application( ))
給我錯誤:'Application()'類型的值不能被轉換爲'Application'。
我是盲人。我剛剛發生格式錯誤。這是固定的。
2.我得到的是,在VS2015帶來了一個網頁的錯誤,上面寫着「電源不可用」,然後類型「System.Reflection.TargetInvocationException」未處理的異常出現在mscorlib.dll
有沒有人有任何想法?
'路過背景之間的Excel變量Workers'這是一個潛在的很多麻煩;你確定你使用的所有'Excel'類型都是'線程安全的'嗎? – Stefan
那麼,錯誤實際上解釋得很好。您的原始變量是「Excel.Application」類型,而「Excel.Application()」是相同類型的數組。因此,如果需要強制轉換,則需要將其轉換爲'Excel.Application' *。除此之外,我不相信這是@Stefan指出的正確方法。我想你應該重新考慮首先傳遞變量的必要性。 –
@Stefan不,我不幸。我仍然在學習編碼,並且不確定線程的安全性。我剛剛做了Google,但我不確定這些Excel類型會帶來什麼。如果這不是將這些從一個背景工作者傳遞給另一個背景工作者的方法,你能否指出我正確的方向? – lolikols