Importing data from excel 2003 file in a gridview in c#

In this article you will see how to import given sheet from excel 2003 file into dataset and then display this data into girdview.

Below is the code for import data from given sheet named Sheet1$ into dataset then bind it to gridview, change sheet name as per your excel sheet name you have to ad suffix $ in sheet name.

Importing data from Sheet1$ of excel 2003 file code -

private void btnImportExcelToGridview_Click(object sender, System.EventArgs e)
        String strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" +
        "Data Source="+Server.MapPath(@"File/Book1.xls")+
        "Extended Properties=Excel 8.0;";

        DataSet ds = new DataSet();
        //You must use the $ after the object
        //you reference in the spreadsheet
        OleDbDataAdapter da = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", strConn);

         Gridview1.DataSource = ds.Tables[0].DefaultView;

Infolink Template