Import Data from Excel to DataGridView in C# or Asp.net

protected void Submit_Click(object sender, EventArgs e)
    {
        try
        {
            DataTable dt = ss();
            gvOne.DataSource = dt;
            gvOne.DataBind();
        }
        catch (Exception ex)
        {
            throw new Exception("Upload Failed: " + ex.Message);
        }

    }


private DataTable ss()
    {
     
       
        string FilePath = @"E:\Excel_Sample\Excel\ActionSchedule1.xlsx";
        string excelConnectString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + FilePath + ";Extended Properties=Excel 12.0;";
        OleDbConnection objConn = new OleDbConnection(excelConnectString);
        OleDbCommand objCmd = new OleDbCommand("Select * From [Sheet1$]", objConn);
        OleDbDataAdapter objDatAdap = new OleDbDataAdapter();
        objDatAdap.SelectCommand = objCmd;
        DataSet ds = new DataSet();
        objDatAdap.Fill(ds);
        //BulkInsertUpload(ds);
        return ds.Tables[0];



    }


<body>
    <form id="form1" runat="server">
    <div>
    <table>
    <tr>
    <td>
    <asp:GridView ID="gvOne" runat="server" >
   
    </asp:GridView>
    </td>
    </tr>
    <tr>
    <td>
    <asp:Button ID="Submit" runat="server" onclick="Submit_Click" Text="Submit" />
    </td>
    </tr>
    </table>
    </div>
    </form>
</body>

Comments