Wednesday, 13 June 2012

Upload Excel File into Dataset,Datatable or Database using Asp.net and C#


Using this Code We can Take both (.xls) format and (.xlsx) format
  try
        {
            string target = Server.MapPath("~/upload");
            if (fileupload1.HasFile)
            {
               fileupload1.SaveAs(System.IO.Path.Combine(target, fileupload1.FileName));
                string connectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + target + "\\" + fileupload1.FileName + ";" + "Extended Properties='Excel 12.0;HDR=YES;'";
                string query = String.Format("SELECT * FROM [{0}$]", "Sheet1");
                OleDbDataAdapter da = new OleDbDataAdapter(query, connectionString);
                DataSet ds = new DataSet();
                da.Fill(ds);
                GVexcel.DataSource = ds.Tables[0];
                GVexcel.DataBind();
           
           
            }
        }
      catch(Exception ex)
        {
            Response.Write(ex.Message);
        }
        finally{
        }
if any problem on this  please let me know...

No comments:

Post a Comment