private void button1_Click(object sender, EventArgs e)
{
DataTable test = getDataFromXLS("c:\temp.xls");
if (test != null)
dataGridView1.DataSource = test;
}
private DataTable getDataFromXLS(string strFilePath)
{
try
{
string strConnectionString = "";
strConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=" + strFilePath + "; Jet OLEDB:Engine Type=5;" + "Extended Properties=Excel 8.0;";
OleDbConnection cnCSV = new OleDbConnection(strConnectionString);
cnCSV.Open();
OleDbCommand cmdSelect = new OleDbCommand(@"SELECT * FROM [Sheet1$]", cnCSV);
OleDbDataAdapter daCSV = new OleDbDataAdapter(); daCSV.SelectCommand = cmdSelect;
DataTable dtCSV = new DataTable();
daCSV.Fill(dtCSV);
cnCSV.Close();
daCSV = null;
return dtCSV;
}
catch (Exception ex)
{
return null;
}
finally
{
}
}
I found that source code to bind an excel file to a DataGridView object on a winform application on the Internet. Yet, I would like to know if there are other ways to do this instead of using ADO- or any SQL-related procedures, thanks for any help.
1 Answer