带有savefiledialog vb.nt的STAThreadAttribute

时间:2014-11-18 13:19:03

标签: vb.net savefiledialog

我有应用程序导出listview到Excel工作表,我试图在后台执行此操作但我在savefiledialog.showdialog()中有错误,这是错误:

System.Windows.Forms.dll中发生了'System.Threading.ThreadStateException'类型的异常,但未在用户代码中处理

附加信息:在进行OLE调用之前,必须将当前线程设置为单线程单元(STA)模式。确保您的Main函数标记了STAThreadAttribute。仅当调试器附加到进程时才会引发此异常。

,这是我的代码:

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
    BackgroundWorker1.RunWorkerAsync()
End Sub

Public Sub saveExcelFile(ByVal FileName As String)
    Try
        Dim xls As New Excel.Application
        Dim sheet As Excel.Worksheet
        Dim i As Integer
        xls.Workbooks.Add()
        sheet = xls.ActiveWorkbook.ActiveSheet
        Dim row As Integer = 1
        Dim col As Integer = 1
        For i = 0 To Me.ListView1.Columns.Count - 1
            sheet.Cells(1, i + 1) = Me.ListView1.Columns(i).Text
        Next
        For i = 0 To Me.ListView1.Items.Count - 1
            For j = 0 To Me.ListView1.Items(i).SubItems.Count - 1
                sheet.Cells(i + 2, j + 1) = Me.ListView1.Items(i).SubItems(j).Text
            Next
        Next

        row += 1
        col = 1

        ' for the header 
        sheet.Rows(1).Font.Name = "Cooper Black"
        sheet.Rows(1).Font.size = 12
        sheet.Rows(1).HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter
        Dim mycol As System.Drawing.Color = System.Drawing.ColorTranslator.FromHtml("#148cf7")
        sheet.Rows(1).Font.color = mycol
        ' for all the sheet without header
        sheet.Range("a2", "z1000").Font.Name = "Arial"
        sheet.Range("a2", "z1000").Font.Size = 13
        sheet.Range("a2", "z1000").HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter

        sheet.Range("A1:X1").EntireColumn.AutoFit()
        sheet.Range("A1:X1").EntireRow.AutoFit()

        xls.ActiveWorkbook.SaveAs(FileName)
        xls.Workbooks.Close()
        xls.Quit()
    Catch ex As Exception

    End Try
End Sub

Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
     Try

        Dim saveFileDialog1 As New SaveFileDialog
        saveFileDialog1.Filter = "Excel File|*.xlsx"
        saveFileDialog1.Title = "Save an Excel File"
        saveFileDialog1.ShowDialog()
        If saveFileDialog1.FileName <> "" Then
            saveExcelFile(saveFileDialog1.FileName)
        End If

    Catch ex As Exception

    End Try
End Sub

Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
    MsgBox("done")
End Sub

2 个答案:

答案 0 :(得分:1)

您可以将有关SaveFileDialog1的所有内容移至Button3_Click并将文件名存储在私有变量中,以便稍后在BackgroundWorker1_DoWork中使用。

Private _filename As String

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
    SaveFileDialog1.Title = "Save Excel File"
    SaveFileDialog1.Filter = "Excel files (*.xls)|*.xls|Excel Files (*.xlsx)|*.xslx"
    SaveFileDialog1.ShowDialog()

    'exit if no file selected
    If SaveFileDialog1.FileName = "" Then
        Exit Sub
    End If

    _filename = SaveFileDialog1.FileName

    BackgroundWorker1.RunWorkerAsync()
End Sub

Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
    ...
    book.SaveAs(_filename)
    ...
End Sub

答案 1 :(得分:0)

我将此代码置于加载形式,一切都很完美:

Control.CheckForIllegalCrossThreadCalls = False