求excel数据导入datagridview同时导入excel数据库的代码!谢谢

解决方案 »

  1.   

    首先引用System.Data.OleDb; 第一步:在窗体加载时初始化相关数据连接 
    定义几个窗体变量: 
    OleDbDataAdapter da = null; 
    OleDbCommandBuilder cb = null; 
    OleDbConnection conn = null; 
    DataSet ds = null;//更新数据库用 
    DataSet ds_xls = null;//显示Excel的内容用的 根据自己的情况将Access数据库文件路径和密码填入到参数 
    private void FormMain_Load(object sender, EventArgs e) 

    string file_name = Application.StarupPath + @"\test.mdb"; 
    string password = "123"; 
    string ConnectionString = string.Format("Provider=Microsoft.JET.OLEDB.4.0;Data Source='{0}';Persist Sercurity Info=false; JET OLEDB:Database Password={1}", file_name, password); 
    conn = new OleDbConnection(ConnectionString); 
    string sql = "SELECT * FROM 你要插入数据的表名 WHERE 1=2";//注意要为表设置一个主键 
    da = new OleDbDataAdapter(sql, conn); 
    ds = new DataSet(); 
    da.Fill(ds); 
    cb = new OleDbCommandBuilder(da); 

    第二步:打开Excel档案并填充数据集 
    这个步骤你有了,填充ds_xls并显示在DataGridView中就可以了。 第三步:开始导入数据 
    假设你的Excel字段结构与Access的字段结构都一样的话。 
    private void btnImport_Click(object sender,EventArgs e) 

        DataTable dt = ds_xls.Tables[0]; 
        for (int i = 0; i < dt.Rows.Count; i++) 
        {              
          ds.Tables[0].Rows.Add(dt.Rows[i].ItemArray); 
          Application.DoEvents(); 
        } 
        da.Update(ds);//在此处更新数据    
        MessageBox.Show("提示操作成功。"); 
    }
      

  2.   

    namespace ExcelToAccess 

        class Program 
        { 
            static void Main(string[] args) 
            { 
                Import(); 
            }         private  static void Import() 
            { 
              OleDbConnection cn = new OleDbConnection("Provider=Microsoft.Jet.OleDb.4.0;data source=OA.xls;Extended Properties='Excel 8.0; HDR=YES; IMEX=1'"); 
                cn.Open(); 
                OleDbCommand cmd = new OleDbCommand("select * from [Sheet1$]", cn); 
                OleDbDataReader read = cmd.ExecuteReader(); 
              Table b = new Table(); 
                while (read.Read()) 
                {                      
                        b.HID =Convert.ToInt32(read .GetValue (0)); 
                        b.Title = read.GetValue(1).ToString() ; 
                      b.Author =read .GetValue (2).ToString (); 
                      b.Contents = read.GetValue(3).ToString();                
                    b.CreatTime =read .GetValue (4).ToString (); 
                    b.Classify = read.GetValue(5).ToString();//取到EXCEL中的值 
                    Add(b);//取到的值加到ACCESS中 
                } 
            }         public static void Add(Table a) 
            { 
                string connStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=omer.mdb;"; 
                string sql = @"insert into tblCustomer(HID,Title,Author,Contents,CreateTime,Classify) values(" + a.HID + ",'" + a.Title + "','" + a.Author + "','" + a.Contents + "','" + a.CreatTime + "','" + a.Classify + "')"; 
                using (OleDbConnection cn = new OleDbConnection(connStr)) 
                { 
                    cn.Open(); 
                    OleDbCommand cmd = new OleDbCommand(sql, cn); 
                    cmd.ExecuteNonQuery(); 
                } 
            }      
        }     /// <summary> 
        /// 映射数据库表 
        /// </summary> 
        class Table 
        { 
            private int _HID;         public int HID 
            { 
                get { return _HID; } 
                set { _HID = value; } 
            } 
            private string _Title;         public string Title 
            { 
                get { return _Title; } 
                set { _Title = value; } 
            } 
            private string _Author;         public string Author 
            { 
                get { return _Author; } 
                set { _Author = value; } 
            } 
            private string _Contents;         public string Contents 
            { 
                get { return _Contents; } 
                set { _Contents = value; } 
            }         private string _CreatTime;         public string CreatTime 
            { 
                get { return _CreatTime; } 
                set { _CreatTime = value; } 
            }         private string _Classify;         public string Classify 
            { 
                get { return _Classify; } 
                set { _Classify = value; } 
            } 
        } 

    自己要在BIN中加OA.xls    omer.mdb  根据语句来建表 不清楚再留言