title: Read Excel File using OLEDB Connection in C#
body: <p>I am trying to read a excel file and put data into a datatable in C#.When I try with Excel 97-2003 file format ( .xls ) it is working fine .But when I try with Excel workbook format (.xlsx ) it is getting a error like following,</p>
  
  <p>"External table is not in the expected format."</p>
  
  <p>Here is my code segment.</p>
  
  <pre><code>string file_name = null;
  
  OpenFileDialog file_d = new OpenFileDialog();
  file_d.Filter = "Excel Files (*.xlsx)|*.xlsx|Excel Files (*.xls)|*.xls";
  file_d.Title = "Select Attendance Record";
  
  if (file_d.ShowDialog() == DialogResult.OK)
  {
      file_name = file_d.FileName;
  }
  
  String ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + file_name + ";Extended Properties=Excel 8.0;";
  
  try
  {
      System.Data.OleDb.OleDbConnection MyConnection;
      System.Data.DataSet DtSet;
      System.Data.OleDb.OleDbDataAdapter MyCommand;
  
      MyConnection = new System.Data.OleDb.OleDbConnection(ConnectionString);
      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];
      MyConnection.Close();
  }
  catch (Exception ex)
  {
      MessageBox.Show(ex.ToString());
  }
  </code></pre>
  
  <p>Please help me with the error.Thanks in advanced.</p>