The C# snippet below will dump ALL sheets of a given Excel file (c:\test.xls) onto the console. It took me a while to figure out how to access sheets within a Excel file which names I don't know - so here it is:
String connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\test.xls;Extended Properties=\"Excel 8.0;HDR=YES;\""; DbProviderFactory factory = DbProviderFactories.GetFactory("System.Data.OleDb"); try { { connection.Open(); DataRowCollection rows = connection.GetSchema("Tables").Rows; foreach (DataRow row in rows) { string sheetName = row["TABLE_NAME"] as string; Console.WriteLine("************************************************************************"); Console.WriteLine("Dumping Sheet: "+sheetName); Console.WriteLine("************************************************************************"); using (DbCommand command = connection.CreateCommand()) { string[] restrictions = { null, null, sheetName, null }; DataRowCollection columns = connection.GetSchema("Columns", restrictions).Rows; for (int i=0; i<columns.Count; i++) { columnNames[i] = columns[i]["COLUMN_NAME"] as string; } Console.Write("Columns in Sheet: "); foreach (string s in columnNames) { Console.Write(s); Console.Write(" "); } Console.WriteLine(""); command.CommandText = "SELECT * from [" + sheetName + "]"; using (DbDataReader dr = command.ExecuteReader()) { while (dr.Read()) { foreach (string columnName in columnNames) { Object fieldValue = dr[columnName]; Console.WriteLine("'" + columnName + "' : " + "'" + fieldValue + "'"); } Console.WriteLine("------------------------------------------------------------------------"); } } } } } } catch (Exception e) { Console.Write(e.StackTrace); }
To be honest - I did not expect it to be that easy. I guess I spent too much time developing Java applications.