【问题标题】:"Save As" Dialog in ASP.Net for exporting an excel file? (service account)ASP.Net 中用于导出 excel 文件的“另存为”对话框? (服务帐号)
【发布时间】:2013-03-11 14:35:28
【问题描述】:

我有一个用于将导出的文件保存到我的 PC/Destination 的代码,但是由于 ASP.NET (4.0) 将显示在运行服务帐户的服务器中,我想让用户在单击按钮1后提示“另存为”对话框,以便他们可以将其保存在各自的文件夹中(在用户的 PC 中,而不是在服务器中)。 (我不能使用环境,因为它会获取服务帐户地址,因为这是在服务器中不可能的。)

注意:要获取用户名,我使用的是 request.servervariables("Logon_User") 并确实在此处查找了一些帖子。 -Exporting datatable to excel file with save dialog -Export DataGridView To Excel with Save Dialog? -Dialog Box for Save As location in VBA Macro

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

【问题讨论】:

  • 这根本不可能。用户无法与服务器上的 UI 交互。
  • 另外,您不应该在服务器上自动化 Office;它不会很好地工作。
  • 对不起,我认为这个问题不清楚..我想保存在用户桌面而不是服务器中。

标签: asp.net vb.net excel excel-interop


【解决方案1】:

您不能让用户与服务器上的 UI 和文件系统进行交互。

相反,您可以将文件保存到服务器上的临时路径,然后通过 HTTP 将其作为文件下载。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-28
    • 2015-01-08
    • 1970-01-01
    • 2021-12-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多