簡體   English   中英

從ASP.NET到Excel的導出-參考文件並打開“另存為”對話框?

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

我有以下代碼:

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

此代碼從我的桌面上的文件夾中抓取一個文件,然后對其進行編輯並將其另存為我的桌面中的新文件“ HI”。

相反,我想將此文件放置在我的項目文件夾中,然后引用它,然后用戶應可以選擇要保存新修改的文​​件的位置。

為此,我需要在代碼中進行哪些修改?

謝謝大家

您應該使用HttpResponse.WriteFile方法。 瀏覽器將自動向用戶顯示對話框。 要在項目內部獲取路徑,可以使用HttpServerUtility.MapPath

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM