views:

532

answers:

3

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)?

A: 

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/

Geoffrey Chetwood
+1  A: 

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.

TheTXI
+2  A: 

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*);
David
SpreadsheetGear also has an IWorkbooks.OpenFromMemory(byte[] data) method so you can read directly from a byte array as well as from a stream as suggested by David.
Joe Erickson