read data from excel

Mar 30 2011 1:53 AM


i use this code for reading the data from excelsheet. but the error" The Microsoft Jet database engine could not find the object ''Sheet1$''.  Make sure the object exists and that you spell its name and the path name correctly " will displayed. plz help.

   private void button1_Click(object sender, EventArgs e)

        {

                                 

                System.Data.OleDb.OleDbConnection MyConnection ;

                System.Data.DataSet DtSet ;

                System.Data.OleDb.OleDbDataAdapter MyCommand ;

                MyConnection = new System.Data.OleDb.OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;Data Source='d:\\csharp.net-informations.xls';Extended Properties=Excel 8.0;");

                MyCommand = new System.Data.OleDb.OleDbDataAdapter("select * from[Sheet1$]", MyConnection);

                MyCommand.TableMappings.Add("Table", "TestTable");

                DtSet = new System.Data.DataSet();

                MyCommand.Fill(DtSet);

                dataGridView1.DataSource = DtSet.Tables[0];

              //  MessageBox.Show("connected");

                MyConnection.Close();

           

           /* catch (Exception ex)

            {

                MessageBox.Show (ex.ToString());

            }*/

        }

 

 

 




Answers (4)