How to read an Excel file into a Datagrid?
This simple code reads in an Excel file and displays it as a datagrid using the OleDbDataAdapter. The first line of your excel file has to have the column headers.
protected void btnImportExcelFile_Click(object sender, EventArgs e)
{
string strConn;
strConn = “Provider=Microsoft.Jet.OLEDB.4.0;” +
“Data Source=C:\\exceltest.xls;” +
“Extended Properties=Excel 8.0;”;
//You must use the $ after the object you reference in the spreadsheet
OleDbDataAdapter myCommand = new OleDbDataAdapter(”SELECT * FROM [Tabelle1$]”, strConn);
DataSet myDataSet = new DataSet();
myCommand.Fill(myDataSet, “ExcelInfo”);
DataGrid1.DataSource = myDataSet.Tables[”ExcelInfo”].DefaultView;
DataGrid1.DataBind();
}