title: How to read the xls and xlsx files using c#
body: <p>How to read the xls and xlsx files using c# <strong>with OpenXML format Without using the OLEDB connection</strong>. I am looking for Open XML format procedure. </p>
  
  <p>Below is the code in which I used the OLEDB preocedure. But I am looking for OpenXML format.</p>
  
  <pre><code>public static DataTable ConvretExcelToDataTable(string FilePath)
  {
      string strConn = string.Empty;
  
       if (FilePath.Trim().EndsWith(".xlsx"))
       {
           strConn = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=\"Excel 12.0 Xml;HDR=YES;IMEX=1\";", FilePath);
       }
       else if (FilePath.Trim().EndsWith(".xls"))
       {
           strConn = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1\";", FilePath);
       }
  
      OleDbConnection conn = null;
      OleDbCommand cmd = null;
      OleDbDataAdapter da = null;
      DataTable dt = new DataTable();
      try
      {
          conn = new OleDbConnection(strConn);
          conn.Open();
          cmd = new OleDbCommand(@"SELECT * FROM [Sheet1$]", conn);
          cmd.CommandType = CommandType.Text;
          da = new OleDbDataAdapter(cmd);
          da.Fill(dt);
      }
      catch (Exception exc)
      {
          Console.WriteLine(exc.ToString());
          Console.ReadLine();
      }
      finally
      {
          if (conn.State == ConnectionState.Open)
              conn.Close();
          conn.Dispose();
          cmd.Dispose();
          da.Dispose();
      }
      return dt;
  }
  </code></pre>
  
  <p>Requirement is to implement the above conversion in <strong>OpenXML format</strong>. Thanks.</p>