Click here to Skip to main content
15,900,378 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to import excel data to sql server database table and display in gridview in asp.net
Posted
Comments
Modi Mayank 17-Mar-15 0:45am    
I think Epplus.dll is the best way to import excel data to datatable. Refer: Import excelsheet data to sql server in C#

 
Share this answer
 
v2
 
Share this answer
 
v4
Comments
I corrected two links. Please correct the first one.
C#
DataTable dt = null;
       if (FileUpload1.HasFile)
       {

           filename = FileUpload1.FileName;
           filename = AppDomain.CurrentDomain.BaseDirectory + @"" + filename;
           var fileExtension = Path.GetExtension(filename).Substring(1);
 if (fileExtension == "xlsx")
                    {
                        {

                            DataSet myDataset = new DataSet();
                            string strConn = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filename + @";Extended Properties=""Excel 12.0 Xml;HDR=YES""";
                            OleDbConnection myData = new OleDbConnection(strConn);
                            try
                            {
                                myData.Open();
                            }
                            catch (Exception ex)
                            {
                                try
                                {
                                    strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + filename + ";" + "Extended Properties=Excel 8.0;HDR=YES;";
                                    myData = new OleDbConnection(strConn);
                                    myData.Open();
                                }
                                catch (Exception ex2)
                                {
                                    strConn = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filename + @";Extended Properties=""HTML Import;HDR=YES;IMEX=1"";";
                                    myData = new OleDbConnection(strConn);
                                    try
                                    {
                                        myData.Open();
                                    }
                                    catch (Exception ex3)
                                    {
                                       
                                    }
                                }
                            }
 if (myData.State.ToString() != "Closed")
                            {
                                int i = 0;
                                DataSet ds = new DataSet();
                                List<string> queryList = new List<string>();
                                foreach (DataRow row in myData.GetSchema("tables").Rows)
                                {
                                    try
                                    {
                                        i++;
                                        string name = row[2].ToString().Replace("''", "'").TrimEnd('_');
                                        OleDbDataAdapter d = new OleDbDataAdapter("SELECT * from [" + name + "]", strConn);
                                        d.Fill(ds);
                                        dt = ds.Tables[0].Copy();                                     
                                      
                                        dt.Namespace = name;
                                        myDataset.Tables.Add(dt);

                                    }
                                    catch (Exception ex)
                                    {
                                    }
                                    break;
                                }

                            }


                        }
                    }

</string></string>



Then you can insert Data into your SQL Database.
C#
BU_UtilDB db = new BU_UtilDB();
                           if (existingrow)
                           {
                               try
                               {
                                   string query = "INSERT INTO [dbo].[Fr_Endkunde]([Anrede],[anrede_id]"
                                       + ",[Firma],[Geburtsdatum],[Name],[Ort],[PLZ] ,[Postfach],[Strasse],[Titel],[Vermittlernummer],[Vorname],[Land])"
                                       + "VALUES('" + dr["Anrede"] + "','" + dr["anrede_id"] + "','" + dr["Firma"] + "','" + datetime + "',"
                                       + "'" + dr["Nachname"] + "','" + dr["Ort"] + "','" + dr["PLZ"] + "','" + dr["Postfach"] + "','" + dr["Strasse"] + "','" + dr["Title"] + "',"
                                       + "'" + dr["Vermittlernummer"] + "','" + dr["Vorname"] + "','" + dr["Land"] + "')";
                                   BU_UtilDB.ExecuteReader(query);

                               }
                               catch (Exception ex)
                               {

                               }
                           }
                       }


In SQL You use your own Column Name. Hope you will got your answer.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900