Fix for issue "The process cannot access the file 'sheet1.xml' because it is being used by another process."

Nov 16, 2012 at 2:15 PM
Edited Nov 16, 2012 at 2:16 PM

There are multiple issues raised with the above error. I think I've found a fix for it, but thought the fix might be lost in amongst all the duplicate issues hence I am adding it to the discussion list rather than replying to one of the many issues. I'm not even sure if this project is still active but a lot of people seem to use it.

Anyway, I spent a while debugging this error when I was getting it and it turns out that it occurred only when a spreadsheet had multiple sheets and any of them were not Read, i.e. just NextResultSet but not read.

The bug is in OpenXmlReader.ReadSheetGlobals because it creates a new stream by overwriting the _sheetStream field, but it does not first check that _sheetStream is closed. So by adding this to the start of that method it works ok.


if (_xmlReader != null) { _xmlReader.Close(); }

if (_sheetStream != null) { _sheetStream.Close(); }


Hope this is useful to someone.