I am developing an application to read an excel spreadsheet, validate the data and then map it to a sql table. The process is to read the file via a streamreader, validate the data, manually make corrections to the excel spreadsheet, validate again — repeat this process until all data validates.
If the excel spreadsheet is open, then when I attempt to read the data via a streamreader I get an error, “The process cannot access the file … because it is being used by another process.” Is there a way to remove the lock or otherwise read the data into a streamreader without having to open and close excel each time?
When you call
File.Opento get the stream are you using the overload that allows you to specifyFileAccess?http://msdn.microsoft.com/en-us/library/y973b725.aspx
Note the parameters:
You can pass
FileAccess.Readto the third param to indicate you only need read-only access. You should also setFileShare.Readto allow others to open the file read-only instead of locking it yourself. Note that if MS Excel opens the file with FileShare.None, you probably wont be able to access it.