简体   繁体   中英

ASP.NET to Excel export - reference file and open save as dialog?

I have this code:

Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click        
    ' Here we go. All the data in the form to an Excel sheet. Cross your fingers...
    Dim oExcel As Object
    Dim oBook As Object
    Dim oSheet As Object

    'Start a new workbook in Excel- this isn't really what I need though, I need it to open a template and populate it.
    oExcel = CreateObject("Excel.Application")
    oBook = oExcel.Workbooks.Open("C:\Users\jagtars\Desktop\HI Resources\HI.xlsx")


    'Add data to cells of the first worksheet in the new workbook
    oSheet = oBook.Worksheets(1)
    oSheet.Range("H5").Value = CustomerName.Text
    oSheet.Range("K5").Value = dropdown_serverVirtualisation.Value
    oSheet.Range("L5").Value = dropdown_desktopVirtualisation.Value
    oSheet.Range("M5").Value = dropdown_oracle.Value
    oSheet.Range("N5").Value = dropdown_sqlServer.Value
    'etc....


    'Save the Workbook and Quit Excel
    oBook.SaveAs("C:\Users\jagtars\Desktop\HI.xlsx")
    MessageBox.Text = "Exported!"
    MessageBox.Focus()
    oExcel.Quit()
End Sub

This code grabs a file from a folder in my desktop and then edits it and saves it as a new file 'HI' in my Desktop.

Instead I would like to place this file in my project folder and then reference it and after that the user should get the option to where they want to save the new modified file.

What do I need to modify in my code to achieve this?

Thanks All.

You should use HttpResponse.WriteFile method. Browser will automatically show dialog to user. To get path inside you project you could use HttpServerUtility.MapPath

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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