问题描述
假设我有一个正在运行的程序,它会定期向.CSV文件添加信息。有没有办法写入文件,而它已经在Excel中打开?很显然,只有在Excel中重新打开文件之后,才会注意到这些更改,但是现在,我正在捕获IOException,并且在当前文件已经打开的情况下启动一个新的.csv文件。 Excel似乎以独占模式打开文件,所以我能想到的唯一方法是将您的更改写入一个临时文件,然后使用 FileSystemWatcher
来查看文件什么时候关闭,然后覆盖这个文件。 不是一个好主意,因为你可能会丢失数据。整个原因excel锁定文件是这样,你不会意外覆盖在Excel中所做的更改。
Suppose I have a program running that periodically adds information to a .CSV file. Is there a way to write to the file while it is already open in Excel? Obviously the changes wouldn't be noticed until the file was re-opened in Excel, but as it stands right now, I'm catching IOException and just starting a new .csv file if the current one is already open.
Excel seems to open the file in exclusive mode, so the only way I can think of would be to write your changes to a temporary file, and then use FileSystemWatcher
to see when the file is closed, and overwrite the file.
Not a very good idea, as you could lose data. The whole reason excel locks the file is so that you don't accidentally overwrite changes made in excel.
这篇关于写入在Excel中打开的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!