ASP.NET到Excel导出 – 参考文件并打开另存为对话框?

我有这个代码:

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方法。 浏览器会自动向用户显示对话框。 要获得你的path项目,你可以使用HttpServerUtility.MapPath