Opening Excel Files Locked by Another User/Process with Telerik SpreadProcessing
Environment
| Version | Product | Author |
|---|---|---|
| 2025.4.1319 | RadSpreadProcessing | Yoan Karamanov |
Description
This article shows how to open a file that is already opened by another user or process. Usually, an exception is received because the file is locked for exclusive access. The solution is to open the file in read-only mode without modifying it.
This knowledge base article also answers the following questions:
- How to open locked files in Telerik SpreadProcessing?
- How to handle locked file exceptions in SpreadProcessing?
- Can SpreadProcessing open files in read-only mode?
Solution
To open a file locked by another user or process, load it into a MemoryStream using a read-only FileStream. This approach allows concurrent access and bypasses the file lock. Follow these steps:
- The FileStream opens the file with read-only access (FileAccess.Read) and allows sharing (FileShare.ReadWrite).
- The MemoryStream receives the file contents, enabling the file lock to be released.
- The XlsxFormatProvider imports the workbook from the MemoryStream.
Here is the code example:
using Telerik.Windows.Documents.Spreadsheet.Model;
using Telerik.Windows.Documents.Spreadsheet.FormatProviders.OpenXml.Xlsx;
Workbook workbook;
XlsxFormatProvider formatProvider = new XlsxFormatProvider();
// Open file with read-only access and allow sharing for reading and writing
using (FileStream fileStream = new FileStream("file.xlsx", FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
// Copy to memory stream to release file lock immediately
using (MemoryStream memoryStream = new MemoryStream())
{
fileStream.CopyTo(memoryStream);
memoryStream.Position = 0;
workbook = formatProvider.Import(memoryStream, null);
}
}