Click here to Skip to main content
15,881,803 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am reading the excel data into datatable and storing into database table.
But i was unable to load the excel data when excel file is opened.
Can anybody give me the solution to read the data from opened excel.

Thanks in advance.
Posted

You can't. The file is locked exclusive by another process no other application can open the file and read it until whatever process has locked the file closes it.
 
Share this answer
 
Comments
Abhinav S 3-Jan-16 0:18am    
I don'[t totally agree with this. An excel can still be copied and the copy can be opened.
FileShare.ReadWrite can also be tried.
Give up and open a copy. Microsoft for some reason made it nearly impossible to do anything with an open workbook except from another workbook. I searched long for a way to communicate with an open workbook, but found nothing but dead ends. Good luck.
 
Share this answer
 
Open the file in readonly mode for e.g. <codelang>File.OpenRead(path).
This way, even if the file is in write-open mode by some other program, the file can still be read.

There will, be, of course, no way to write to the file.
 
Share this answer
 
Comments
Dave Kreskowiak 2-Jan-16 23:48pm    
Actually, that's wrong. The mode of the open operation has nothing to do with the file being locked. What DOES has affect it is if the file is opened with Shared Access. By default, a file is opened with exclusive access. No other process can access the file so long as it's open exclusive. A file must be opened with Shared.Read access for another application to be able to open it in a ReadOnly mode and ONLY if the 2nd application requests a ReadOnly mode.

With Excel being used as a database file being read by OleDb, the file must be opened with Read AND Write access. This will cause the operation to fail in the file is locked or does not specify Shared.All.
Abhinav S 3-Jan-16 0:17am    
Well in that case, the excel can still be copied.
So a copy of the excel can still be opened!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900