Epplus Csharp | How to manipulate already opened(in use) Excel File

橙三吉。 提交于 2019-12-05 10:02:00

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.

Ron Chibnik

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);
  • path is the path to the file that you would otherwise use for FileInfo
  • FileMode says you want to open it
  • FileAccess says you want to read it
  • FileShare says that other programs can read/write.

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().

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!