1

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?

Fred
  • 3,365
  • 4
  • 36
  • 57
jooooooooota
  • 35
  • 1
  • 8

2 Answers2

1

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.

Community
  • 1
  • 1
Yair I
  • 1,133
  • 1
  • 6
  • 9
  • 1
    On a previous project, I had to cut out Interop.Excel to something faster, as it was super slow for out growing reports. OpenXml does lack the polish and style when creating sheets and docs, but with a bit of wrapping and head trauma, the style can be worked back in. – DarceVader Apr 23 '19 at 11:19
  • But i want to open Excel window, I need the user see the document – jooooooooota Apr 23 '19 at 14:36
  • I didn't know you wanted him to see it on excel. the user need only to see the document in excel or he need to edit and save in excel? – Yair I Apr 24 '19 at 08:59
1

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!

TheBv
  • 150
  • 6
  • @jooooooooota Oh you want to display everything in Excel... I don't think that's possible your best bet would be to either save the file which you explicitly said you didn't want to do or if the document was encrypted within excel you could open the password protected excel file like this : https://stackoverflow.com/a/37232899/8581150 If you just want the user to look at the data and not interact too much with it you could also load the data into a Windows Form table. I don't have any other ideas. Sorry! – TheBv Apr 23 '19 at 20:45