Your code needs a very little fix:
string extension = Path.GetExtension(openFileDialog1.FileName);
Previously your GetExtension was trying to find an extension on your connection string, witch is impossible for the method, because is a connection string and not a path!
So your code fixed should look like this:
string extension = Path.GetExtension(openFileDialog1.FileName);
using (System.Data.OleDb.OleDbConnection con = new System.Data.OleDb.OleDbConnection())
{
switch (extension)
{
case ".xls":
string xlsconStr = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source='" + openFileDialog1.FileName + "'; Extended Properties=Excel 8.0;";
con.ConnectionString = xlsconStr;
break;
case ".xlsx":
case ".xlsm":
string xlsxconStr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" + openFileDialog1.FileName + "'; Extended Properties=Excel 12.0;";
con.ConnectionString = xlsxconStr;
break;
}
using (System.Data.OleDb.OleDbCommand oconn = new System.Data.OleDb.OleDbCommand("SELECT * FROM [" + listBox1.SelectedIndex.ToString() + "$]", con))
{
con.Open();
System.Data.OleDb.OleDbDataAdapter adapter = new System.Data.OleDb.OleDbDataAdapter(oconn);
System.Data.DataTable data = new System.Data.DataTable();
adapter.Fill(data);
dataGridView1.DataSource = data;
}
}
Now your code will enter the switch case statement in order to initialize you connection string ;)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…