This article provides answer to questions asked about how to connect to an excel file as a data source for a given application, I provide the solution via this ten steps walkthrough.
I wrote this article to answer a question asked about how to connect to an excel file as a data source for a given application, I provide the solution via this ten steps walkthrough:
Walkthrough:
Let's begin by creating an Excel file and configuring an ODBC connection
private void btnConnect_Click(object sender, EventArgs e)
{
OdbcConnection oConn = new OdbcConnection();
oConn.ConnectionString = "Dsn=EXCEL";
OdbcCommand oComm = new OdbcCommand();
oComm.Connection = oConn;
oComm.CommandText = "Select * From [Feuil1$A1:C3]";
try
DataSet ds = new DataSet();
OdbcDataAdapter oAdapter = new OdbcDataAdapter(oComm);
oConn.Open();
oAdapter.Fill(ds);
dataGridView1.DataSource = ds;
dataGridView1.DataMember = ds.Tables[0].TableName;
label1.Text = "Connection established successfully!!!";
}
catch (IOException caught) { MessageBox.Show(caught.Message); }
catch (OdbcException caught) { MessageBox.Show(caught.Message); }
finally
oConn.Close();
Good dotneting!!!
Pro WPF: Windows Presentation Foundation in .NET 3.0