像访问Sql一样访问Excel收藏

DataSet KDDataSet = new DataSet();

System.Data.OleDb.OleDbConnection objConn = new System.Data.OleDb.OleDbConnection("Provider=Microsoft.jet.OLEDB.4.0;Data Source=" + filename + ";Extended Properties=Excel 8.0");//这个更据不同的Excel版本不一样

//filename 为Excel的路径
            objConn.Open();
            //DataTable schemaTable = objConn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, null);
           // String tableName = schemaTable.Rows[0][2].ToString().Trim();
            String SqlStr = "Select * from [Sheet1$]";
            System.Data.OleDb.OleDbCommand command = new System.Data.OleDb.OleDbCommand(SqlStr, objConn);
            System.Data.OleDb.OleDbDataAdapter DAP = new System.Data.OleDb.OleDbDataAdapter(command);

 DAP.Fill(KDDataSet, "Sheet1");

//剩下的就是ADO的操作了,不用讲了吧

posted @ 2009-02-25 15:44  丁守庆  阅读(126)  评论(0编辑  收藏  举报
专业成就价值