string filePath = @"c:\Test.xls";
            string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath + ";Extended Properties='Excel 8.0;HDR=Yes;IMEX=1;'";

            string strSheetName = "Info";
            //选择Excel文件中的一个sheet,比如叫Info
            string strExcel = "select * from  [" + strSheetName + "$] ";
            //string strExcel = "select * from [Info$] ";

            //打开连接,读取Excel文件中数据到Dataset中,然后关闭连接
            OleDbConnection conn = new OleDbConnection(strConn);
            conn.Open();
            OleDbDataAdapter adapter = new OleDbDataAdapter(strExcel, conn);
            DataSet ds = new DataSet();
            adapter.Fill(ds, "data");
            conn.Close();

            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                string eID = ds.Tables[0].Rows[i]["序号"].ToString();
                string eName = ds.Tables[0].Rows[i]["纳税人识别号"].ToString();
                string eDate = ds.Tables[0].Rows[i]["纳税人名称"].ToString();
                string ePosition = ds.Tables[0].Rows[i]["法人代表"].ToString();
                if (ePosition.Equals("测试用户"))
                    Console.WriteLine("序号:{0},纳税人识别号:{1},纳税人名称:{2},法人代表:{3}", eID, eName, eDate, ePosition);
            }

posted on 2009-03-20 11:46  JGD  阅读(153)  评论(1)    收藏  举报