How to open an Excel document that is already open in MS-Excel?

3 Aug 20211 minute to read

Syncfusion XlsIO do support opening an Excel document that is already open in Microsoft Excel. But the approaches are different in .NET Framework and .NET Standard.

OpenReadOnly method can be used in .NET Framework whereas FileShare.ReadWrite overload should be used while loading the file into file stream in .NET Standard. The following code snippet explains this.

using (ExcelEngine excelEngine = new ExcelEngine())
{
    IApplication application = excelEngine.Excel;
    application.DefaultVersion = ExcelVersion.Excel2016;

    IWorkbook workbook = application.Workbooks.OpenReadOnly("Template.xlsx");

    workbook.SaveAs("Output.xlsx");
}
using (ExcelEngine excelEngine = new ExcelEngine())
{
    IApplication application = excelEngine.Excel;
	application.DefaultVersion = ExcelVersion.Excel2016;
	
	FileStream inputStream = new FileStream("Template.xlsx", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
	IWorkbook workbook = application.Workbooks.Open(inputStream);
	
	FileStream outputStream = new FileStream("Output.xlsx", FileMode.Create, FileAccess.ReadWrite);
	workbook.SaveAs(outputStream);
}