使用oledb读取excel表

string path = "C:\\Users\\aaa\\Desktop\\aa.xls";
            string conn = "Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source =" + path + ";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;

            MessageBox.Show(count+"");

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

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

                tUserID = ds.Tables["[Sheet1$]"].Rows[i][0].ToString().Trim();

                tUserName = ds.Tables["[Sheet1$]"].Rows[i][1].ToString().Trim();
                tDept = ds.Tables["[Sheet1$]"].Rows[i][2].ToString().Trim();

                MessageBox.Show(tUserID + "," + tUserName + "," + tDept);


            }

 

posted @ 2015-07-24 18:11  lunawzh  阅读(254)  评论(0编辑  收藏  举报