使用C#Interop从Excel中以明文forms获取所有工作表名称?

我正在使用VS2010 + Office Interop 2007来尝试从5到6页的Excel电子表格中获取一些特定的电子表格名称。 我所做的只是在制表符分隔的文本文件中保存所需的一些电子表格,以供进一步处理。 所以对于我得到的三个电子表格名称,每个名称都有自己的制表符分隔的文本文件。

我可以通过Interop将文件保存为制表符分隔,但假设我知道给定的页面名称是什么。 我已经被告知,每个页面名称不会遵循严格的命名惯例,但是在寻找所需的名称时,我可以考虑多个名称,例如“RCP”,“rcp”,“Recipient”等。

我的问题是,我可以在某种索引中获得所有的电子表格页面名称,所以我可以遍历它们并尝试find我需要的三个名称? 这将比尝试通过bajillion try / catches来抓取“RCP”,“rcp”,“Recipient”页面好得多。

我很接近,因为我可以通过以下方式在Excel电子表格中获得COUNT个页面:

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 

感谢您的时间。

 foreach ( Worksheet worksheet in excelWorkbook.Worksheets ) { MessageBox.Show( worksheet.Name ); } 

你可以用字典:

 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 );