Hi NerakSeven,
Please refer below sample and below article links-
HTML
<div>
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="btnImpor" runat="server" Text="Import" OnClick="btnImpor_Click" />
<asp:GridView ID="gvCustomers" runat="server">
</asp:GridView>
</div>
Namespaces
C#
using System;
using System.Configuration;
using System.Data;
using System.Data.OleDb;
using System.IO;
Code
C#
protected void btnImpor_Click(object sender, EventArgs e)
{
string FileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
string Extension = Path.GetExtension(FileUpload1.PostedFile.FileName);
string FilePath = FileUpload1.PostedFile.FileName;
DataTable dt = new DataTable();
string conStr = "";
switch (Extension)
{
case ".xls": //Excel 97-03
conStr = ConfigurationManager.ConnectionStrings["Excel03ConString"].ConnectionString;
break;
case ".xlsx": //Excel 07
conStr = ConfigurationManager.ConnectionStrings["Excel07ConString"].ConnectionString;
break;
}
conStr = String.Format(conStr, FilePath, "yes");
using (OleDbConnection connExcel = new OleDbConnection(conStr))
{
using (OleDbCommand cmdExcel = new OleDbCommand())
{
using (OleDbDataAdapter odaExcel = new OleDbDataAdapter())
{
cmdExcel.Connection = connExcel;
connExcel.Open();
DataTable dtExcelSchema;
dtExcelSchema = connExcel.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
string sheetName = dtExcelSchema.Rows[0]["TABLE_NAME"].ToString();
connExcel.Close();
connExcel.Open();
cmdExcel.CommandText = "SELECT * From [" + sheetName + "B6:D10]";
odaExcel.SelectCommand = cmdExcel;
odaExcel.Fill(dt);
connExcel.Close();
}
}
}
this.gvCustomers.DataSource = dt;
this.gvCustomers.DataBind();
}
Screenshot