Here there is a similar question In memory of OleDbConnection to an Excel file , but answered this question, completely avoiding it, doing it differently.
Here is a sample code that uses OleDbConnection
to access an Excel file from disk:
static void Main(string[] args) { String filePathToExcelFile = "c:\\excelfile.xls"; Boolean hasHeaders = true; String connectionString = String.Format( "Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};" + "Extended Properties=\"Excel 12.0;HDR={1};IMEX=2\"", filePathToExcelFile, hasHeaders ? "Yes" : "No"); using(OleDbConnection conn = new OleDbConnection(connectionString)) using (OleDbCommand command = new OleDbCommand("SELECT * FROM [Sheet1$]", conn)) { conn.Open(); OleDbDataReader datareader = command.ExecuteReader(); while(datareader.Read()) { Object[] values = new object[datareader.FieldCount]; datareader.GetValues(values); Console.WriteLine(String.Join(",", values)); } } }
I would like to extract an Excel file from a NON-SEEKABLE System.IO.Stream
, and not a persistent file on disk.
The question is divided into two parts: a) Can I "point" OleDbConnection to System.IO.Stream? b) If so, can it be only a direct stream, not a search one?
FYI: if you want to run this piece of code, you will need to install the Microsoft Access Database Engine 2010 Redistributable . And if you install the 64-bit version, you will need to target the project to x64 and vice versa.
source share