ADO.net 读取Excel 时,是否可以将Excel里的数据读到DataReader中?
ADO.net 读取Excel 时,是否可以将Excel里的数据读到DataReader中?
------解决方案--------------------建议将问题转发到.Net开发区,解决起来会比较快。
------解决方案--------------------
------解决方案--------------------
 OpenFileDialog dialog = new OpenFileDialog();
           if (dialog.ShowDialog() == DialogResult.OK)
           {
               string strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties='Excel 8.0;HDR=YES;IMEX=1;';Data Source=" + dialog.FileName + ";";
               OleDbConnection connection = new OleDbConnection(strConnection);
               connection.Open();
               try
               {
                   string str = "Select * from [Sheet1$]";
                   OleDbCommand myCommand = new OleDbCommand(str, connection);
                   OleDbDataReader myReader;
                   myReader = myCommand.ExecuteReader();
                   while (myReader.Read())
                   {
                       MessageBox.Show(myReader.GetString(0));
                   }
                   myReader.Close();
               }
               catch (Exception ex)
               {
                   MessageBox.Show(ex.Message);
               }
               finally
               {
                   connection.Close();
               }
           }
一个小测试 ,实践证明是可以的