-1

I have a C# code that connects to Excel database via OpenFileDialog func. Excel file has too many worksheets and I would like to get all their names in one array and add them to combobox. For example, this one is the first Excel document. Picture

Any suggestions?

  • Possible duplicate of https://stackoverflow.com/questions/1164698 – D Stanley Aug 16 '18 at 19:46
  • Dearest, welcome to Stack Overflow! Please, before asking your question do a research in SO database to find an anser so in this way, you avoid making duplicates questions. Thank you and at your service! – Sergio Rezende Aug 16 '18 at 19:49
  • Excel is not a database. If you meant to get tables in Excel then look at OleDbConnection.GetSchema. Else you can simply loop the worksheets collection to get their names. – Cetin Basoz Aug 16 '18 at 19:52
  • Have you heard of the Accord package? Maybe Accord.IO.ExcelReader will be of use to you, there is a way to take all the sheets and make them into a list I believe. if you want them as a string array let me know and Ill show you how to do that – G. LC Aug 16 '18 at 19:54
  • That's pretty easy to do with the OpenXml SDK (well, for some definition of "easy") – Flydog57 Aug 16 '18 at 20:18

1 Answers1

1

You could try the following:

Accord.IO.ExcelReader reader = new Accord.IO.ExcelReader("yourFile.xlsx");
string[] worksheets = reader.GetWorksheetList();

make sure to include

using Accord.IO;

which you can get Accord from NuGet by searching Accord and Accord.IO.

Then just fill in the items of the Combo box

foreach (string worksheet in worksheets)
{
    myComboBox.Items.Add(worksheet);
}
G. LC
  • 794
  • 1
  • 8
  • 27