How to read open excel file at C#

我与影子孤独终老i 提交于 2019-11-29 05:58:49

I think you can still copy the file while excel has it open, so you could make a copy of the file and then open that. Just make sure you clean up after yourself when you are done with the copy.

splintor

You need to open it with FileShare.ReadWrite:

FileStream stream = File.Open("myfile.xlsx", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);

See this answer.

You could use the Interop library to use the already opened instance of Excel.

oExcel == (Excel.Application) System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application")

You can try the File.Open with a fourth parameter - fileShare.

FileStream stream = File.Open("myfile.xlsx", FileMode.Open, FileAccess.Read, FileShare.Read);

You may need to specify write access also.

Somedeveloper

To ensure that correct opening and closing of the file please look at using the c# using statements

using (FileStream stream = File.Open("myfile.xlsx", FileMode.Open, FileAccess.Read)) 
{

}

To open the same file more than once at the same time, it needs to be opened in shared mode.

Hope this may help others.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!