Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read an in-memory Excel file (byte array) with ADO.NET?

I want to use ADO.net to extract some data from an Excel file. This process is pretty well documented on the internet. My catch is that my file has been uploaded by a user, and so exists only as a byte array in memory. For security and performance reasons I would rather not write this file to disk.

Is there a way of constructing a connection string that connects to a byte array? Or perhaps exposing that array as a file that is actually stored in memory (like a RAM disk I guess)?

like image 455
Jack Ryan Avatar asked May 28 '09 12:05

Jack Ryan


3 Answers

You can't connect if it only exists in memory. OLE is also ruled out (though using Office Automation for a server application is poor design to begin with).

The only way I can think of is to read the binary Excel data yourself - for example use SpreadSheetGear.Net with something like:

SpreadsheetGear.Factory.GetWorkbookSet().Workbooks.OpenFromStream(*stream*);
like image 90
David Avatar answered Sep 28 '22 15:09

David


Like Rich B said in his answer, I do not think it is possible to connect in a standard ado.net way to an excel file that is just hanging around in memory. The simplest work-around would probably be to actually save the excel file to the disk, connect to it using the Jet engine with your connection string, and then when you are done performing all your tasks, get rid of the file. This may not be the ideal in terms of performance, but it is lacking that certain WTFiness which would cause you to pull your hair out.

like image 24
TheTXI Avatar answered Sep 28 '22 17:09

TheTXI


I don't think it is possible to do this via just a connection string.

If you want help with an Excel file on disk: http://connectionstrings.com/excel-2007

In general: http://connectionstrings.com/

like image 36
GEOCHET Avatar answered Sep 28 '22 15:09

GEOCHET