OpenXML and opening a file in Read only mode

September 09, 2014 by Anuraj

.Net .Net 4.0 ASP.Net Windows Forms

In one of my project, I am using OpenXML SDK for opening Excel files. Recently I got an issue like SpreadsheetDocument.Open() method was throwing an IOException, if the Excel file is opened by MS Excel, even if I set the isEditable parameter false.

IO Exception - Open XML opening file in Read Only mode

I fixed this problem by passing a stream instead of string (file path). And I used the File stream class to open the Excel file. Here is the code snippet.

using (var fileStream = new FileStream(fileName, FileMode.Open, 
    FileAccess.Read, FileShare.ReadWrite))
{
    using (var spreadSheetDocument = SpreadsheetDocument.Open(fileStream, false))
    {
        //Implementation
    }
}

Happy Coding :)

Copyright © 2024 Anuraj. Blog content licensed under the Creative Commons CC BY 2.5 | Unless otherwise stated or granted, code samples licensed under the MIT license. This is a personal blog. The opinions expressed here represent my own and not those of my employer. Powered by Jekyll. Hosted with ❤ by GitHub