I am using the ACE OLEDB connection string to connect to an excel file. I've noticed my query (see example below) that returns the column schema takes longer to run when the worksheet has more rows of data on it.
For some of my larger worksheets (200k rows) it is taking around 10 seconds for the header schema to be returned. It there a way to speed this up or a better way to get the column headers?
string connectionString = string.Empty;
connectionString = String.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};
Extended Properties=""Excel 12.0 Xml;HDR=YES;IMEX=1""", path);
OleDbConnection con = new OleDbConnection(connectionString);
con.Open();
DataTable dtSchema = new DataTable();
System.Diagnostics.Debug.WriteLine("Start: " + DateTime.Now.ToLongTimeString());
dtSchema = con.GetOleDbSchemaTable(OleDbSchemaGuid.Columns,
new Object[] { null, null, WorksheetName, null });
System.Diagnostics.Debug.WriteLine("End: " + DateTime.Now.ToLongTimeString());
con.Close();
UPDATE
I tried rewriting this - turning Headers off and manually reading only the first row. It still takes around 10 seconds to process on my larger files (small ones still come back very quickly). Is there anything else I can try that might be able to get the header(first row) values quicker?
string connectionString = string.Empty;
connectionString = String.Format(@"Provider=Microsoft.ACE.OLEDB.12.0; Data Source={0};
Extended Properties=""Excel 12.0 Xml;HDR=NO;""", path);
DataTable dtSchema = new DataTable();
using (OleDbConnection conn = new OleDbConnection(connectionString))
{
OleDbCommand command = new OleDbCommand(String.Format("SELECT * FROM [{0}A1:II1]", WorksheetName),conn);
OleDbDataAdapter dataAdapter = new OleDbDataAdapter();
dataAdapter.SelectCommand = command;
DataSet dataSet = new DataSet();
dataAdapter.Fill(dataSet);
dtSchema = dataSet.Tables[0];
}