title: asp.net - Reading Excel Sheet Names Not Returning Data Tables
body: <p>You might redirect me to other links for this one, but I'll be the first to tell you guys: I'VE USED THIS FOR MANY TIMES and I really don't know what's the problem this time.</p>
  
  <p>My scenario is: I need to import Excel data into the database. Sounds easy, right? Now, first I need to know the sheet names. That's where my problem started. And I repeat, I've used it for multiple times, and I don't know what I'm doing wrong this time:</p>
  
  <p>The 'it' I'm referring to is this piece of code:</p>
  
  <pre><code>public string[] GetSheetNames(string excelPath) {
  
              try
              {
  
              string[] ar = null;
  
              if (Path.GetExtension(excelPath) == ".xls")
              {
                  conString = String.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + excelPath + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"");
              }
  
              else if (Path.GetExtension(excelPath) == ".xlsx")
              {
                  conString = String.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + excelPath + ";Extended Properties=\"Excel 12.0 Xml;HDR=YES\";");
              }
  
                  using (oleConn = new OleDbConnection(conString))
                  {
                      oleConn.Open();
  
                      if (oleConn.State == ConnectionState.Open)
                      {
  
                          //DataTable dt = oleConn.GetSchema("Tables");
  
                          //DataTable dt = oleConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new Object[] {null, null, null, "TABLE"});
  
                          DataTable dt = oleConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
  
                          if (dt == null)
                          {
                              return null;
                          }
  
                          ar = new string[dt.Rows.Count];
                          int t = 0;
  
                          foreach(DataRow dr in dt.Rows){
  
                              ar[t] = dr["TABLE_NAME"].ToString();
                              t++;
                          }
  
  
                      }
  
  
                      return ar;
                  }
  
              }
              catch (Exception)
              {
  
                  throw;
              }
          }
  </code></pre>
  
  <p>It throws an Exception, saying that the DataTable's row count is 0. I also used <code>DataTable dt = oleConn.GetSchema("Tables")</code> and </p>
  
  <pre><code>DataTable dt = oleConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, 
            new Object[] {null, null, null, "TABLE"}); 
  </code></pre>
  
  <p>but they all returned the same.</p>
  
  <p>What could be the problem? Could it be the Excel file? But I've tried other Excel files and they all failed.</p>