Monday 26 December 2011

import Excel data to Gridview in c#

Place one file upload control and grid view on webform
In file upload click event place the below code
        protected void btnUpload_Click(object sender, EventArgs e)
        {
          if (FileUpload1.HasFile)
            {
                string FileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
                string Extension = Path.GetExtension(FileUpload1.PostedFile.FileName);
                //string FolderPath = ConfigurationManager.AppSettings["FolderPath"] + "\\";
                string FolderPath = ConfigurationManager.AppSettings["FolderPath"];
                string path = Path.GetFullPath(FileName);
               //string FilePath = Server.MapPath(FolderPath + FileName);
               // string FilePath = Server.MapPath(("App_Data/" + Extension));
               string SaveLocation = FolderPath + "ExcelUpload4";
               FileUpload1.SaveAs(SaveLocation);
               if (grdbom_detail.Rows.Count == 0)
               {
                   Import_To_Grid(SaveLocation, Extension);
               }
            

        }

//Method to return data to gridview
     private void Import_To_Grid(string FilePath, string Extension)
    {
       
        string conStr="";

       
             if (Extension == ".xls" || Extension == ".xlsx")
        {
            switch (Extension)
            {
                case ".xls": //Excel 97-03
                    conStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + FilePath + ";;Extended Properties=Excel 8.0;";
                    break;
                case ".xlsx": //Excel 07
                    conStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + FilePath + ";;Extended Properties=Excel 8.0;";
                    break;

            }
            //string ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + FilePath + ";;Extended Properties=Excel 8.0;";
            //conStr = String.Format(conStr, FilePath);


            OleDbConnection connExcel = new OleDbConnection(conStr);
            OleDbCommand cmdExcel = new OleDbCommand();
            OleDbDataAdapter oda = new OleDbDataAdapter();
            DataTable dt1 = new DataTable();
            cmdExcel.Connection = connExcel;

            //Get the name of First Sheet
            connExcel.Open();
            DataTable dtExcelSchema;
            dtExcelSchema = connExcel.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
            string SheetName = dtExcelSchema.Rows[0]["TABLE_NAME"].ToString();
          
            int y = dtExcelSchema.Rows.Count;

            connExcel.Close();
          
            {
                //Read Data from First Sheet
                connExcel.Open();
                cmdExcel.CommandText = "SELECT * From [" + SheetName + "]";
                oda.SelectCommand = cmdExcel;
                oda.Fill(dt1);
              
                for (int i = 0; i < dt1.Rows.Count; i++)
                {
                   
                   
                   //Loop throgh elements and add to list
                }
                connExcel.Close();

                //Bind Data to GridView
                // GridView1.Caption = Path.GetFileName(FilePath);
                grd_detail.DataSource = list;
                grd_detail.DataBind();
                SaveToSession("Ex_object", er);
            }
           
        }
        else
        {
            string sMessage = " Please Load .xls and .xslx type Files";
            ScriptManager.RegisterStartupScript(this, typeof(Page), "Alert", "<script>alert('" + sMessage + "')</script>", false);
        }
    }

No comments:

Post a Comment