Task: I need to load the contents of an Excel spreadsheet worksheet into a datagridview using C#.
Progress: I've connected to an Excel spreadsheet through the OLEdbConnection and I need to reference the first worksheet by worksheet index in my select statement. (I will not know what the name of the worksheet will be, but we will ALWAYS load the first worksheet.) Below is the code I have so far...
tbFileName.Text = openFileDialog1.FileName;
System.Data.OleDb.OleDbConnection MyConnection;
System.Data.DataSet DtSet;
System.Data.OleDb.OleDbDataAdapter MyCommand;
MyConnection = new System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + tbFileName.Text + ";Extended Properties=Excel 12.0;");
//MyCommand = new System.Data.OleDb.OleDbDataAdapter("select * from [FlexAccountView$]", MyConnection); <-- accessing worksheet by name...
**MyCommand = new System.Data.OleDb.OleDbDataAdapter("select * from " + [need worksheet index reference here], MyConnection);**
I know this is easy, but for some reason I'm drawing a blank today. I've searched sOf, but haven't been able to spot what I'm looking for. Any help would be greatly appreciated. Thank you for your assistance.