Eddy V
Eddy V

Reputation: 99

"Save As" Dialog in ASP.Net for exporting an excel file? (service account)

I have a code working for saving the exported file into my PC/Destination however since the ASP.NET (4.0) will be displayed in a server running a service account, i would like to let the user after he clicks the button1 to prompt "Save as" dialog so they can save it in their respective folder (In the user's PC, not server). (i can not use environment since it will grab the service account address, since this is in the server its not possible.)

Note: to get the user name i'm using request.servervariables("Logon_User") and did look for some posts here. -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

Upvotes: 0

Views: 1776

Answers (1)

SLaks
SLaks

Reputation: 887453

You cannot have the user interact with UI and filesystem on the server.

Instead, you can save the file to a temporary path on the server, then serve it as a file download over HTTP.

Upvotes: 1

Related Questions