Epplus Csharp | How to manipulate already opened(in use) Excel File. it works only when Excel file is closed. I can do like closing before code execution and reopen again, but do not think it's a way to go as my file contains more than 50000 rows (file size is big). Please advise, how to figure it out.
Thanks in advance
Rather than using a FileInfo to open the ExcelPackage workbook, use a stream:
FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
ExcelPackage pkg = new ExcelPackage(fs);
With the workbook specified by path open in excel, you will be able to read lines from the file, but will get an exception if you try to call ExcelPackage.Save()
.
It is only possible, if the file is opened for reading by a third-party, but not for writing.
If the third-party opened the file for writing, your attempt to open the same file even just for reading will result in System.IO.IOException
. In fact, it's all about FileStream
and not specific to EpPlus and ExcelPackage. Example in terms of .NET:
var fileStream1 = new FileStream(@"File.ext", FileMode.Open, FileAccess.Read);
var fileStream2 = new FileStream(@"File.ext", FileMode.Open, FileAccess.Read);
will work just fine. But the second line of the following fragment:
var fileStream1 = new FileStream(@"File.ext", FileMode.Open, FileAccess.Write);
var fileStream2 = new FileStream(@"File.ext", FileMode.Open, FileAccess.Read);
will result in a System.IO.IOException
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With