直接添代码:

 public void connExcel(string strPath)
        {
            //string strConn = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + strPath + ";Extended Properties='Excel 8.0;HDR=YES;IMEX=1';";
            string strConn = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + strPath + ";Extended Properties='Excel 12.0;HDR=YES;IMEX=1';";
            
            OleDbDataReader myDataReader = null;
            OleDbConnection myOleDbConnection = new OleDbConnection(strConn);
            OleDbCommand myOleDbCommand = new OleDbCommand("SELECT * FROM [Sheet1$]", myOleDbConnection);

            try
            {
                myOleDbConnection.Open();
                myDataReader = myOleDbCommand.ExecuteReader();
                while (myDataReader.Read())
                {
                    Console.WriteLine("序号:" + myDataReader.GetValue(0));//列1
                    Console.WriteLine("标题:" + myDataReader.GetValue(1));//列2
                    Console.WriteLine("预期结果:" + myDataReader.GetValue(2));//列3
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            finally
            {
                // Always call Close when done reading.
                if (myDataReader != null)
                    myDataReader.Close();

                // Close the connection when done with it.
                if (myOleDbConnection != null)
                    myOleDbConnection.Close();
            }
        }
View Code

相关文章: