1
votes

I am trying to retrieve a list of the worksheets in an Excel workbook, but the collection I get back has both the sheet names and the data column id's, which seem to be called 'Defined Names' in the original xlsx xml. Can you tell me how to return only the worksheet names?

The code I'm using is along the lines of:

OleDbConnection connExcel = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;"
            + @"Data Source=" + FilePath + ";"
            + @"Extended Properties=""Excel 8.0;HDR=Yes;""");

OleDbCommand cmdExcel = new OleDbCommand();
cmdExcel.Connection = connExcel;
connExcel.Open();

DataTable testTable = connExcel.GetSchema("Tables");

The contents of the resulting testTable collection contain entries under TABLE_NAME of:

  • DATA1
  • DATA2
  • DATA3
  • DATA4
  • DATA5
  • Sheet1$
  • TEST1 -TEST2
  • TESTHKEY
  • TESTKEYS
  • TESTVKEY

They all have a TABLE_TYPE of TABLE.

The original workbook corresponding to the above would have 1 worksheet containing 5 columns, the first row would contain a header. I'm only interested in the Sheet1$ entry. The spreadsheet is created in Excel 2010, I'm trying to process it in an ASP.NET 4 app written in C#. Potentially, the worksheet name may have been changed so I can't guarrantee that it will always be Sheet1$.

2

2 Answers

3
votes

My first thoughts were wrong so I came up with this as a workaround. The actual worksheet names returned should always end with $, so I hacked it to check for that. Messy but you get the general idea I'm sure.

OleDbConnection connExcel = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;"
        + @"Data Source=c:\test.xlsx;"
        + @"Extended Properties=""Excel 12.0 Xml;HDR=Yes;""");

        OleDbCommand cmdExcel = new OleDbCommand();
        cmdExcel.Connection = connExcel;
        connExcel.Open();

        DataTable testTable = connExcel.GetSchema("Tables");

        String[] excelSheets = new String[testTable.Rows.Count];
        int i = 0;

        foreach (DataRow row in testTable.Rows)
        {
            excelSheets[i] = row["TABLE_NAME"].ToString();

            if (excelSheets[i].EndsWith("$"))
            {
                Console.WriteLine(excelSheets[i] = row["TABLE_NAME"].ToString());
                i++;
            }
            else
            {
                i++;
            }

        }

        Console.ReadLine();
-1
votes
private static string EXCEL_CONNECTIONSTRING = string.Format("Provider=Microsoft.ACE.OLEDB.12.0; data source={0}; Extended Properties=Excel 12.0;", "#{FILENAME}");

private IEnumerable<string> GetWorksheetNames(string excelFile)
{

     var currentConnectionString = EXCEL_CONNECTIONSTRING.Replace("#{FILENAME}", excelFile);

     using (OleDbConnection connection = new OleDbConnection(currentConnectionString))
     {
        OleDbCommand cmdExcel = new OleDbCommand();


        cmdExcel.Connection = connection;
        connection.Open();

        DataTable dt = connection.GetSchema("Tables");

        IEnumerable<string> excelSheets = dt.Rows.Cast<DataRow>().Select(row => row["TABLE_NAME"].ToString());
        dt.Dispose();
        connection.Close();
        return excelSheets;

     }

  }