Home » excel » c# – Is there a way to get byte[] from ExcelWorkBook without saving it to disk first

c# – Is there a way to get byte[] from ExcelWorkBook without saving it to disk first

Posted by: admin May 14, 2020 Leave a comment

Questions:

I’m using Microsoft.Office.Interop.Excel to create Excel file in C#, append data, change format and all.
Now I save data into file, but what I want is to write bytes into stream, so basically I don’t want to write it into file then open it and then write it into stream.

I just want to write data from Excel Workbook into byte array and then return it.

So what I do now is

xlWorkBook.SaveAs(path, Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
            xlWorkBook.Close(true, misValue, misValue);
            xlApp.Quit();

and I want to say something like this xlWorkBook.GetBytes();. So byte[] would be equal as byte[] readed from excel file with FileStream.

Thanks in advance.

How to&Answers:

There is no way to do this, except save as file then read it with filestream