简体   繁体   English

使用保存文件对话框从数据表中导出Excel

[英]Export an excel from my datatable with a Savefile dialog

I am using VB.Net. 我正在使用VB.Net。 I want to export an excel from my datatable with a Savefile dialog. 我想使用“保存文件”对话框从数据表中导出Excel。 But I get error as: 但我得到错误为:

"Current thread must be set to single thread apartment (STA) mode before OLE calls can be made. Ensure that your Main function has STAThreadAttribute marked on it. This exception is only raised if a debugger is attached to the process." “在进行OLE调用之前,必须将当前线程设置为单线程单元(STA)模式。确保在Main函数上标记了STAThreadAttribute。仅当将调试器附加到进程时,才会引发此异常。”

I have googled alot, but for my "Web application" it could not be resolved. 我已经在Google上搜索了很多,但是对于我的“ Web应用程序”却无法解决。

here is my code: 这是我的代码:

 Dim saveFileDialog1 As New SaveFileDialog()
    saveFileDialog1.Filter = "Excel File|*.xls"
    saveFileDialog1.FilterIndex = 2
    saveFileDialog1.RestoreDirectory = True

    If saveFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK _
   Then
        ExportExcel(dt, "Appraisals" + value.ToString())
    End If

I Can't Find the error of your code but you can try mine =D 我找不到您的代码错误,但可以尝试我的= D

Dim workBookName As String = "Computers"

If Not workBookName = "" Then
   SaveFileDialog1.Filter = "Excel WorkBook|*.xlsx"
   SaveFileDialog1.ShowDialog()
   Location = SaveFileDialog1.FileName
   ExportExcel(dt, "Appraisals" + value.ToString())
Else
   MsgBox("Please enter Worksheet Name", MsgBoxStyle.Critical, "Export To Excel")
End If

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM