简体   繁体   English

从C#启动Excel并在“保存”上关闭它

[英]Launch Excel from C# and close it on “Save”

I want to do the following with C# and Microsoft Excel: 我想用C#和Microsoft Excel执行以下操作:

1 - The user chooses a file. 1 - 用户选择文件。

2 - Micorosft Excel is shown to edit that file. 2 - 显示Micorosft Excel编辑该文件。

3 - As soon as the user clicks Excel's "Save" button Microsoft Excel should close.The user shouldn't have to click on exit. 3 - 用户单击Excel的“保存”按钮后,Microsoft Excel应该关闭。用户不必单击退出。

Any idea on #3 ? 关于#3的任何想法?

Regards, Sebastian 此致,塞巴斯蒂安

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. 您可以让Excel宏处理BeforeSave事件,取消用户启动的保存,将文件保存在宏中,保存后您将进入宏,然后可以关闭Excel。

So maybe something like: 也许是这样的:

Private Sub myBeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    If Not HandlingBeforeSave Then
        HandlingBeforeSave = True
        Cancel = True
        Me.Save
        Application.Quit    
    End If
End Sub

This kb article describes adding a Macro to Excel from C#. 这篇 kb文章描述了从C#向Excel添加宏。

You could handle the Workbook 's BeforeSave event, then run a timer that checks the Saved property every ten seconds, and closes Excel if it's true . 你可以处理WorkbookBeforeSave事件,然后运行检查定时器Saved财产每十秒钟,并关闭Excel中,如果它是true

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. 关闭Excel时应谨慎,因为如果您关闭他们正在处理的(不同)文件,您的用户会非常生气。

My Interop is a bit rusty, but I think there was an event that Excel fires when it saves a file. 我的Interop有点生疏,但我认为在保存文件时Excel会触发一个事件。

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! 想象一下,当用户在应用程序之外使用Excel时,如果保存处理程序处于活动状态会发生什么 - 他点击了save,并且Excel消失了!

Also you may use FileSystemWatcher class to catch up saving moment, and kill Excel on it. 此外,您可以使用FileSystemWatcher类来赶上节省时间,并在其上杀死Excel。 Of course, if you know where user have to save file. 当然,如果您知道用户必须保存文件的位置。

You could use VSTO for Excel. 您可以使用VSTO for Excel。 Gives you the ability to write C# code against the Excel object model. 使您能够针对Excel对象模型编写C#代码。 Should be able to do exactly what you want. 应该能够做到你想要的。

Still though, SWeko has a good point. 尽管如此,SWeko还是有一个好处。 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. 如果不是每次保存都会关闭Excel。

I had a similar issue, however I wait until the user closes Excel. 我有类似的问题,但我等到用户关闭Excel。 See my "solution" at Excel automation: Close event missing . Excel自动化中查看我的“解决方案” :关闭事件丢失

I find it somehow unintuitive to close excel when the user hits "save". 当用户点击“保存”时,我发现以某种方式关闭excel是不直观的。 If you insist on the "save" event, you might want to watch the file metadata change as soon as the user saves (eg the last modified date). 如果您坚持“保存”事件,则可能希望在用户保存后立即观察文件元数据更改(例如,上次修改日期)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM