且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在一个位置保存Excel并显示位置消息

更新时间:2022-12-26 17:43:20

该代码在服务器上运行 。该文件保存在服务器上。除非你允许你的用户控制你的服务器,告诉他们路径不会对他们有任何好处。



它可能出现从Visual Studio运行站点时工作,但这只是因为客户端和服务器在特定情况下是同一台计算机。



此外,不支持使用ASP.NET中的Office Interop,并且可能无法在您的服务器上运行:



Microsoft目前不推荐也不支持Microsoft Office应用程序自动化来自任何无人参与的非交互式客户端应用程序或组件(包括ASP,ASP.NET,DCOM和NT服务),因为Office可能表现出不稳定的行为和/或死亡Office在此环境中运行时锁定。





要将文件发送到客户端,您需要保存它到临时位置,使用 Response.TransmitFile 发送文件,然后删除临时文件:

  Dim  tempFileName 作为 字符串 = Path.Combine(Path.GetTempPath(),Path.GetRandomFileName()+  。xslx
xlWorkBook.SaveAs(tempFileName)
xlWorkBook.Close()
xlApp.Quit()

releaseObject(xlApp)
releaseObject(xlWorkBook)
releaseObject(xlWorkSheet)
releaseObject(xlWorkSheet2)

' 设置正确的内容类型:
Response.ContentType = application / vnd.openxmlformat s-officedocument.spreadsheetml.sheet

' 设置建议的文件名:
Dim fileName 作为 字符串 = 字符串 .Format( ClaimReport_ {0:yyyyMMdd_HH_mm} .xlsx,DateTime.Now)
Dim disposition As 字符串 = 字符串 .Format( 附件; filename ={0},fileName)
Response.AppendHeader( 内容处理,处置)

' 发送文件:
Response.TransmitFile(tempFileName)
Response.Flush()

' 删除临时文件:
File.Delete(tempFileName)

' 不要将当前页面的输出与文件一起发送:
Response.SuppressContent = True
Application.CompleteRequest()


Good morning Experts,

I have a web application. I am trying to save a grid into an Excel. My code works absolutely fine in save the Excel. After saving I wanted to show the path where it saved. For example it is saved on your desktop with file name. I have below code to save the file in a particular path. Now I want to save the file on Desktop with my filename. And also want to show a pop up saying that it is saved on desktop with this name.

Dim strDate = DateTime.Now.ToString("yyyyMMdd_HH_mm")
xlWorkBook.SaveAs("C:\ClaimReport_" + strDate + ".xlsx")
xlWorkBook.Close()
xlApp.Quit()

releaseObject(xlApp)
releaseObject(xlWorkBook)
releaseObject(xlWorkSheet)
releaseObject(xlWorkSheet2)

ScriptManager.RegisterStartupScript(Me.Page, Me.GetType(), "alert", "Data exported to Excel Succesfully in C:\ClaimReport_" + strDate + ".xlsx ", True)



Could someone help me with this?

That code is running on the server. The file is saved on the server. Unless you're going to allow your users to take control of your server, telling them the path won't do them any good.

It might appear to work when you run the site from Visual Studio, but that's only because the client and server are the same computer in that particular case.

Also, using Office Interop from ASP.NET is not supported, and will probably not work on your server:


Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment.



To send the file to the client, you will need to save it to a temporary location, use Response.TransmitFile to send the file, and then delete the temporary file:

Dim tempFileName As String = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName() + ".xslx")
xlWorkBook.SaveAs(tempFileName)
xlWorkBook.Close()
xlApp.Quit()
 
releaseObject(xlApp)
releaseObject(xlWorkBook)
releaseObject(xlWorkSheet)
releaseObject(xlWorkSheet2)

' Set the correct content type:
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"

' Set the suggested file name:
Dim fileName As String = String.Format("ClaimReport_{0:yyyyMMdd_HH_mm}.xlsx", DateTime.Now)
Dim disposition As String = String.Format("attachment; filename=""{0}""", fileName)
Response.AppendHeader("Content-Disposition", disposition)

' Send the file:
Response.TransmitFile(tempFileName)
Response.Flush()

' Delete the temporary file:
File.Delete(tempFileName)

' Don't send the output of current page with the file:
Response.SuppressContent = True
Application.CompleteRequest()