Use this
protected void btnUpload_Click(object sender, EventArgs e)
{
try
{
if (fUpload.HasFile)
{
string Path = Server.MapPath(fUpload.FileName);
FillDatasetExcel(Path);
}
}
catch (Exception ex)
{
throw ex;
}
}
public DataTable FillDatasetExcel(string filepath)
{
string path = filepath;
string connectionstring =
@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties='Excel 12.0 Xml;HDR=YES;FMT=Delimited';";
byte[] Bytes = fUpload.FileBytes;
File.WriteAllBytes(path, Bytes);
OleDbConnection connection = new OleDbConnection();
connection.ConnectionString = connectionstring;
OleDbCommand command = new OleDbCommand();
command.CommandType = CommandType.Text;
command.Connection = connection;
command.CommandText = "Select * FROM [Sheet1$]";
connection.Open();
DataTable dt = new DataTable();
dt.Load(command.ExecuteReader());
connection.Close();
return dt;
}
}