从 C# 启动 Excel 并在“保存”上将其关闭
我想使用 C# 和 Microsoft Excel 执行以下操作:
1 - 用户选择一个文件。
2 - 显示 Microrosft Excel 来编辑该文件。
3 - 一旦用户单击 Excel 的“保存”按钮,Microsoft Excel 就会关闭。用户不必单击退出。
对 #3 有什么想法吗?
问候, 塞巴斯蒂安
I want to do the following with C# and Microsoft Excel:
1 - The user chooses a file.
2 - Micorosft Excel is shown to edit that file.
3 - As soon as the user clicks Excel's "Save" button Microsoft Excel should close.The user shouldn't have to click on exit.
Any idea on #3 ?
Regards,
Sebastian
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您可以让 Excel 宏处理
BeforeSave
事件,取消用户启动的保存,将文件保存在宏中,保存后您将位于宏中,然后可以关闭 Excel。因此,可能类似于:
这篇 kb 文章介绍了从 C# 添加宏到 Excel。
You could have a Excel macro handle the
BeforeSave
event, cancel the save initiated by the user, save the file in the macro and after saving you'd be in your macro and could then close Excel.So maybe something like:
This kb article describes adding a Macro to Excel from C#.
您也可以使用 FileSystemWatcher 类来赶上保存时间,并杀死 Excel。当然,如果您知道用户必须保存文件的位置。
Also you may use FileSystemWatcher class to catch up saving moment, and kill Excel on it. Of course, if you know where user have to save file.
我的 Interop 有点生疏,但我认为 Excel 保存文件时会触发一个事件。
然而,我认为这是一个非常危险的功能,因为根据我的经验,互操作往往有点不确定:)。想象一下,如果当用户在应用程序之外使用 Excel 时保存处理程序处于活动状态,会发生什么情况 - 他单击“保存”,Excel 就消失了!
My Interop is a bit rusty, but I think there was an event that Excel fires when it saves a file.
However, I think this is a very dangerous feature to implement, since, in my experience, interop tends to be a bit non-deterministic :). Imagine what will happen if the save handler is active when the user is using Excel outside of your application - he clicks on save, and Excel dissapears!
您可以使用 VSTO for Excel。使您能够针对 Excel 对象模型编写 C# 代码。应该能够准确地做你想做的事。
不过,SWeko 有一个很好的观点。您将需要弄清楚如何确定它是否应该在保存时关闭。如果不这样做,每次保存都会关闭 Excel。
You could use VSTO for Excel. Gives you the ability to write C# code against the Excel object model. Should be able to do exactly what you want.
Still though, SWeko has a good point. You will need to figure out how to determine if it is supposed to close on save. If you don't every save would close Excel.
您可以处理
工作簿
的BeforeSave
事件,然后运行一个计时器,每十秒检查一次Saved
属性,如果为true
,则关闭 Excel。关闭 Excel 时应小心谨慎,因为如果您关闭用户正在处理的(不同的)文件,他们会非常生气。
You could handle the
Workbook
'sBeforeSave
event, then run a timer that checks theSaved
property every ten seconds, and closes Excel if it'strue
.You should exercise caution when closing Excel, since your users will be very angry if you close a (different) file that they're working on.
我不确定这是否有帮助,但我认为它很接近您的需求
http://www.c-sharpcorner.com/UploadFile/jodonnell/ Excel2003fromCSharp12022005021735AM/Excel2003fromCSharp.aspx
I am not sure if this helps, but I think it is close to your need
http://www.c-sharpcorner.com/UploadFile/jodonnell/Excel2003fromCSharp12022005021735AM/Excel2003fromCSharp.aspx
我遇到了类似的问题,但是我等到用户关闭 Excel。请参阅我的“解决方案”:Excel 自动化:关闭事件丢失。
我发现当用户点击“保存”时关闭 Excel 有点不直观。如果您坚持“保存”事件,您可能希望在用户保存后立即观察文件元数据的变化(例如上次修改日期)。
I had a similar issue, however I wait until the user closes Excel. See my "solution" at Excel automation: Close event missing.
I find it somehow unintuitive to close excel when the user hits "save". If you insist on the "save" event, you might want to watch the file metadata change as soon as the user saves (e.g. the last modified date).