简体   繁体   中英

Open workbook in Interop.Excel from byte array

I want to open Excel from byte[] because my file is encrypted and I want to open after decrypt but without write in a file.

The office has "restricted access" and I want to open my file with this protection but without saving the decrypted content in a file.

myApp.Workbooks.Open only supports a path.

Is it possible?

It is not possible because the interop is actually an interface for programs to run and operate existing excel on the computer.

I think you need to use openxml created by Microsoft to work with excel word and PowerPoint.

DocumentFormat.OpenXml

Then you can use:

ExcelPackage excelPackage = new ExcelPackage(stream)

or

var pck = new OfficeOpenXml.ExcelPackage();
pck.Load(File.OpenRead(path));

pck.Load(Stream) can use any stream as input not only from a file.

It depends on your needs.

As an alternative to OpenXml there's also ExcelDataReader which from my experience is a lot faster in processing data compared to Interop.Excel(around 3 times+).

It can also open encrypted Excel files directly( stackoverflow ) The github page for ExcelDataReader has some great examples on how to use it. The only thing you'd have to do is:

This:

using (var stream = File.Open(filePath, FileMode.Open, FileAccess.Read))

Becomes this:

using (var stream = new MemoryStream(yourByte[])

And if you just want to open the password protected excel file you'd do this:

var conf = new ExcelReaderConfiguration { Password = "yourPassword" }; //Add this

excelReader = ExcelReaderFactory.CreateReader(stream, conf); //change the excel Reader to this

Make sure to check the Github page for more info!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM