我有一个代码用于将导出的文件保存到我的 PC/Destination 但是由于 ASP.NET (4.0) 将显示在运行服务帐户的服务器中,我想让用户在单击 button1 后提示“另存为”对话框,以便他们可以将其保存在各自的文件夹中(在用户的 PC 中,而不是服务器中)。(我不能使用环境,因为它会获取服务帐户地址,因为这在服务器中是不可能的。)
注意:要获取用户名,我使用的是 request.servervariables("Logon_User") 并确实在此处查找了一些帖子。-使用保存对话框将数据表导出到 Excel 文件 -使用保存对话框将 DataGridView 导出到 Excel? - VBA 宏中另存为位置的对话框
Dim cnn As SqlConnection
Dim connectionString As String
Dim sql As String
Dim i, j As Integer
Dim xlApp As Excel.Application
Dim xlWorkBook As Excel.Workbook
Dim xlWorkSheet As Excel.Worksheet
Dim misValue As Object = System.Reflection.Missing.Value
xlApp = New Excel.Application
xlWorkBook = xlApp.Workbooks.Add(misValue)
xlWorkSheet = xlWorkBook.Sheets("sheet1")
connectionString = "Data Source=xxxx"
cnn = New SqlConnection(connectionString)
cnn.Open()
sql = "SELECT * FROM ControlCharts"
Dim dscmd As New SqlDataAdapter(sql, cnn)
Dim ds As New DataSet
dscmd.Fill(ds)
For i = 0 To ds.Tables(0).Rows.Count - 1
For j = 0 To ds.Tables(0).Columns.Count - 1
xlWorkSheet.Cells(i + 1, j + 1) = _
ds.Tables(0).Rows(i).Item(j)
Next
Next
'here instead of saving it to the C drive, let the user choose.
xlWorkSheet.SaveAs("C:\\ControlCharts.xlsx")
xlWorkBook.Close()
xlApp.Quit()
releaseObject(xlApp)
releaseObject(xlWorkBook)
releaseObject(xlWorkSheet)
cnn.Close()
CheckBox1.Checked = False