博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

excel导入sql

Posted on 2010-06-11 11:55  gzlxm  阅读(1437)  评论(0)    收藏  举报

private void InputExcel(string pPath)

        {

            string conn = "Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source =" + pPath + ";Extended Properties='Excel 8.0;HDR=False;IMEX=1'";

            OleDbConnection oleCon = new OleDbConnection(conn);

            oleCon.Open();

            string Sql = "select * from [Sheet1$]";

            OleDbDataAdapter mycommand = new OleDbDataAdapter(Sql, oleCon);

            DataSet ds = new DataSet();

            mycommand.Fill(ds, "[Sheet1$]");

            oleCon.Close();

            int count = ds.Tables["[Sheet1$]"].Rows.Count;

            for (int i = 0; i < count; i++)

            {

                string tUserID, tUserName, tDept, tEmail, tLeader, tAngent;

                tUserID = ds.Tables["[Sheet1$]"].Rows[i]["員工代號"].ToString().Trim();

                tUserName = ds.Tables["[Sheet1$]"].Rows[i]["員工姓名"].ToString().Trim();

                tDept = ds.Tables["[Sheet1$]"].Rows[i]["所屬部門代號"].ToString().Trim();

                tEmail= ds.Tables["[Sheet1$]"].Rows[i]["E-Mail Address"].ToString().Trim();

                tLeader = ds.Tables["[Sheet1$]"].Rows[i]["直属主管"].ToString().Trim();

                tAngent = ds.Tables["[Sheet1$]"].Rows[i]["代理人"].ToString().Trim();

                string excelsql = "insert into " + this.UserInfo.Company + "..[resak] (resak001, resak002, resak015,resak005,resak013,resak009) values ('" + tUserID + "','" + tUserName + "','" + tDept + "','" + tEmail + "','" + tLeader + "','" + tAngent + "')";

                DBCommand cmd = DscDBData.GetDataDBCommand();

                cmd.ExeNonQuery(excelsql);

            }

        }