Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read open excel file at C#

I want to read already open excel file with C#. I am using this method but it can't read the excel file while the file is open in Microsoft excel.

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

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

I hope you understands what I mean. I want to keep excel file open and while file is open at Microsoft excel i want to read it from C#. I am using C# net framework 4.0

like image 363
MonsterMMORPG Avatar asked Feb 14 '11 12:02

MonsterMMORPG


1 Answers

You need to open it with FileShare.ReadWrite:

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

See this answer.

like image 65
splintor Avatar answered Oct 09 '22 00:10

splintor