如何在一个位置保存Excel并显示位置消息 [英] how to Save Excel in a location and showing the message of location

查看:134
本文介绍了如何在一个位置保存Excel并显示位置消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

早上好专家,



我有一个网络应用程序。我想将网格保存到Excel中。我的代码在保存Excel时非常好。保存后我想显示它保存的路径。例如,它以文件名保存在桌面上。我有以下代码将文件保存在特定路径中。现在我想用我的文件名将文件保存在桌面上。并且还想显示一个弹出窗口,说它是用这个名字保存在桌面上的。



  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( .Page, GetType (), alert 数据导出到Excel成功地在C:\ Clai中mReport _ + strDate + 。xlsx





有人可以帮我吗?

解决方案

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



它可能出现从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()


这篇关于如何在一个位置保存Excel并显示位置消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆