excel表sheet1,字段为A,B,C。
SQL数据库表table,字段为A,B,C,D,E。
求将sheet1数据插入SQL数据库表table的完整代码。
本人菜鸟,希望大家耐心给出详细的代码和注释,能让我不怎么费劲就能运行起来,小弟感激不尽。

解决方案 »

  1.   

    時間關係,思路可以說說:讀取Excel資料到DataTable對象,然後循環,insert
      

  2.   

    看一下,偶博客里有收藏一篇高效的,从Excel到SqlServer的导入方法,4~5秒就可以导入好几万记录http://blog.csdn.net/Linux7985/archive/2009/03/05/3959716.aspx
      

  3.   

    excel 本质就是一个表,你就当做和datatable是一样的,去excel表中循环取出数据,然后插入datatable中即可。代码,网上一大堆。
      

  4.   

    excel表格到DATATABLE
     public System.Data.DataTable SheettoDatatable(string FilePath,string sheetname)
            { 
            if (!File.Exists(FilePath))
                {
                    throw new Exception("Excel文件不存在!");
                }
                //RegistryKey key = Registry.LocalMachine;
                //RegistryKey software = key.OpenSubKey("software\\Microsoft\\Jet\\4.0\\Engines\\Excel\\TypeGuessRows",true); //该项必须已存在
                //software.SetValue("TypeGuessRows", "0");            System.Data.DataTable dt=new System.Data.DataTable();;           
                OleDbConnection dbcon = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + FilePath + ";Extended Properties=Excel 8.0");
                if (dbcon.State == ConnectionState.Closed)
                {
                 dbcon.Open();
                }
                try
                {
                  OleDbCommand cmd = new OleDbCommand("select * from [" sheet1+ "$]", dbcon);
                  OleDbDataAdapter adapter = new OleDbDataAdapter(cmd);
                  adapter.Fill(dt);               
                }
                catch (Exception exp)
                        {
                            MessageBox.Show(exp.Message);
                        }
             
                finally
                {
                    if (dbcon.State == ConnectionState.Open)
                    {
                        dbcon.Close();
                    }
                }
                return dt;    
            }DATATABLE 到数据库  sql语句自己修改吧        public void DTInserttoDataBase(DataTable DT,string DataBaseTableName)
            {
                try
                {
                    //若数据库连接的当前状态是关闭的,则打开连接
                    if (MyConn.State == ConnectionState.Closed)
                    {
                        MyConn.Open();
                    }
                    for (int i = 0; i < DT.Rows.Count; i++)
                    {
                        string insertsql = "INSERT into " + DataBaseTableName + " values";
                        string str2="(";
                        for(int j=0;j<DT.Columns.Count;j++)
                        {
                            str2=str2+"'"+DT.Rows[i][j]+"',";
                         
                        }
                        str2 = str2.Substring(0, str2.Length - 1);
                        str2=str2+")";
                        insertsql =insertsql+str2;
                        //MessageBox.Show(insertsql);
                        SqlCommand command = new SqlCommand(insertsql, MyConn);
                        command.ExecuteNonQuery();    
                    }                
                }
                 catch (Exception ex)
                {
                    //strError = "数据插入失败:" + e.Message;
                    MessageBox.Show(ex.Message, "数据插入失败!");
                   
                }
                finally
                {
                    if (MyConn.State != ConnectionState.Closed)
                    {
                        MyConn.Close();
                    }
                }
            }
      

  5.   

    http://topic.csdn.net/u/20110406/16/26753bf7-7c08-458c-a939-5420ab82ade2.html?8320参考!