我想打开一个excel文件,等待用户编辑并保存 [英] I want to open an excel file and wait for user to edit and save it

查看:264
本文介绍了我想打开一个excel文件,等待用户编辑并保存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想打开一个excel文件,等待用户编辑并保存,然后我使用数据。

I want to open an excel file and wait for user to edit and save it, and then i use the data.

我使用这个代码: p>

I am using this code :

        string tempFileName = "test.xlsx";
        MemoryStream ms = DocumentFormat.OpenXml.Extensions.SpreadsheetReader.Create();
        DocumentFormat.OpenXml.Extensions.SpreadsheetWriter.StreamToFile(tempFileName, ms);

        var psi = new ProcessStartInfo(tempFileName);
        var p = new Process();
        p.StartInfo = psi;
        p.Start();

        p.WaitForExit();
        p.Close();
        // use the data

,一切看起来都可以。
但问题是,如果在运行此块之前有一个打开的excel文件,我将在p.WaitForExit()上有一个错误;行是无效的操作异常。

and everything looks ok. but the problem is, if there is an open excel file before running this block i will have an error on p.WaitForExit(); line which is "Invalid Operation Exception".

我认为原因是:如果有一个名为excel.exe的进程,我的代码不会再创建一个等待它退出,并将合并进程或类似的东西。
我可以强制用户关闭所有打开的excel文件,然后运行这个代码,或者我自己杀死excel.exe进程,并从我的代码重新启动它。我不想使用这些解决方案。

i think the reason is: if there is a already a process named "excel.exe" my code wont create another one to wait for it to be exited, and will merge the processes or something like that. i can force the user to close all open excel documents and then run this code, or i my self kill excel.exe process and start it again from my code. which i don't want to use these solutions.

请帮我找出办法!
非常感谢

please help me to find a way out! Thanks a lot

推荐答案

当Excel打开电子表格时,Excel会保留与电子表格相同的文件夹中的文件,并且只要用户打开Excel电子表格,该文件就存在。文件名(Excel 2007)与电子表格文件名相同,但以〜$为前缀,只需轮询该文件,或者使用电子表格文件夹上的FileSystemWatcher查找它,从而消失 - 这表示用户已停止使用电子表格并关闭它。

When Excel has a spreadsheet open, Excel maintains a file in the same folder as the spreadsheet, and this file exists for as long as the user has the Excel spreadsheet open. The file name (Excel 2007) is the same as your spreadsheet file name but prefixed with "~$", simply poll for that file existing or use a FileSystemWatcher on the spreadsheet folder to look for it disappearing - this indicates the user has ceased using the spreadsheet and closed it.

这篇关于我想打开一个excel文件,等待用户编辑并保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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