【问题标题】:ASP.NET to Excel export - reference file and open save as dialog?ASP.NET 到 Excel 导出 - 参考文件并打开另存为对话框?
【发布时间】:2013-06-19 13:58:14
【问题描述】:

我有这个代码:

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”。

相反,我想将此文件放在我的项目文件夹中,然后引用它,然后用户应该可以选择保存新修改文件的位置。

我需要在我的代码中修改什么来实现这一点?

谢谢大家。

【问题讨论】:

  • 您必须在精神上断开运行在服务器上的 ASP.NET 代码隐藏与客户端浏览器的连接,而客户端浏览器将位于其他任何地方。

标签: asp.net vb.net excel export-to-excel


【解决方案1】:

您应该使用HttpResponse.WriteFile 方法。浏览器将自动向用户显示对话框。要获取项目内部的路径,您可以使用HttpServerUtility.MapPath

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多