Get all table names in text format from Excel using C # Interop?

I am using VS2010 + Office Interop 2007 to try to get multiple spreadsheet names from an Excel spreadsheet with 5-6 pages. Everything that I do allows me to save those few tables that I need in a text file with tab delimiters for further processing. So for the three table names that I get, each of them will have its own tab delimited text file.

I can save the file as a separator as a separator only through Interop, but on condition that I know what the name of this page is. I was told that every page name would not follow a strict naming convention, but I can take into account several names such as "RCP", "rcp", "Recipient", etc. When searching for the desired name.

My question is, can I get all the table names in some index so that I can repeat them and try to find the three names that I need? This would be much nicer than trying to grab the "RCP", "rcp", "Recipient" pages with bajillion try / catch.

I am close because I can get COUNT pages in an Excel spreadsheet using the following:

Excel.Application excelApp = new Excel.Application(); // Creates a new Excel Application excelApp.Visible = true; // Makes Excel visible to the user. // The following code opens an existing workbook string workbookPath = path; Excel.Workbook excelWorkbook = null; try { excelWorkbook = excelApp.Workbooks.Open(workbookPath, 0, false, 5, "", "", false, Excel.XlPlatform.xlWindows, "", true, false, 0, true, false, false); } catch { //Create a new workbook if the existing workbook failed to open. excelWorkbook = excelApp.Workbooks.Add(); } // The following gets the Worksheets collection Excel.Sheets excelSheets = excelWorkbook.Worksheets; Console.WriteLine(excelSheets.Count.ToString()); //dat count 

Thank you for your time.

+7
c # excel excel-interop
source share
1 answer
 foreach ( Worksheet worksheet in excelWorkbook.Worksheets ) { MessageBox.Show( worksheet.Name ); } 

You can use the dictionary:

 Dictionary<string, Worksheet> dict = new Dictionary<string, Worksheet>(); foreach ( Worksheet worksheet in excelWorkbook.Worksheets ) { dict.Add( worksheet.Name, worksheet ); } // accessing the desired worksheet in the dictionary MessageBox.Show( dict[ "Sheet1" ].Name ); 
+11
source share

All Articles