如何在C#上阅读打开的excel文件

Mon*_*RPG 13 c# excel file-io ioexception

我想用C#读取已打开的excel文件.我正在使用此方法,但它在Microsoft Excel中打开文件时无法读取excel文件.

FileStream stream = File.Open("myfile.xlsx", FileMode.Open, FileAccess.Read);
Run Code Online (Sandbox Code Playgroud)

它给 IOException: The process cannot access the file 'myfile.xlsx' because it is being used by another process.

我希望你明白我的意思.我想保持excel文件打开,当文件在Microsoft Excel打开时,我想从C#中读取它.我正在使用C#net framework 4.0

spl*_*tor 21

您需要使用FileShare.ReadWrite打开它:

FileStream stream = File.Open("myfile.xlsx", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
Run Code Online (Sandbox Code Playgroud)

看到这个答案.


Joh*_*ner 6

我认为你仍然可以复制文件,而excel打开它,所以你可以复制该文件,然后打开它.完成复制后,请确保自己清理干净.

  • 不,这不是解决方案.它在这里:http://stackoverflow.com/a/898017/48082 (3认同)