How to display list of tabs in an Excel spreadsheet using C#?

rwheeler23
rwheeler23 used Ask the Experts™
on
I have a client who sends me spreadsheets full of customer data. Up until now it was always one spreadsheet per customer. Now they are putting each of their  customer's data on a  separate tab inside one spreadsheet. How do I now update my C# code to give them a selection box for each tab and then read the data in the tab chosen?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Senior Software Engineer
Top Expert 2009
Commented:
Use Microsoft.Office.Interop.Excel: (This library depends on which version of Excel you are using)

using Excel = Microsoft.Office.Interop.Excel;

Excel.Application xlApp = new Excel.Application();
Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(@"Book1.xlsx");
foreach (Excel.Worksheet displayWorksheet in xlWorkbook.Worksheets)
{
    Debug.Writeline(displayWorksheet.Name);
}

Open in new window

rwheeler23President

Author

Commented:
Thanks. They have a mixture of Excel 2010 and 2013. I will tell them to always use the same one. I suppose a logical follow-up question would be is a way to determine which version they are using?
Miguel OzSenior Software Engineer
Top Expert 2009

Commented:
No the interop works OK with older versions as well, just add the 2013 assembly to your project and you should  be fine
rwheeler23President

Author

Commented:
'Thanks

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial