使用 C# Interop 从 Excel 中以纯文本形式获取所有工作表名称?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18788091/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get all worksheet names in plaintext from Excel with C# Interop?
提问by Franklin_Skipdiddle
I'm using VS2010 + Office Interop 2007 to attempt to get a few specific spreadsheet names from an Excel spreadsheet with 5-6 pages. All I am doing from there is saving those few spreadsheets I need in a tab delimited text file for further processing. So for the three spreadsheet names I get, each one will have its own tab delimited text file.
我正在使用 VS2010 + Office Interop 2007 尝试从 5-6 页的 Excel 电子表格中获取一些特定的电子表格名称。我所做的就是将我需要的那几个电子表格保存在一个制表符分隔的文本文件中以供进一步处理。所以对于我得到的三个电子表格名称,每个名称都有自己的制表符分隔的文本文件。
I can save a file as tab delimited just fine through Interop, but that's assuming I know what the given page name is. I have been informed that each page name will not follow a strict naming convention, but I can account for multiple names like "RCP", "rcp", "Recipient", etc when looking for a desired name.
我可以通过 Interop 将文件保存为制表符分隔的文件,但前提是我知道给定的页面名称是什么。我被告知每个页面名称都不会遵循严格的命名约定,但在查找所需名称时,我可以考虑多个名称,如“RCP”、“rcp”、“收件人”等。
My question is, can I get all spreadsheet page names in some sort of index so I may iterate through them and try to find the three names I need? That would be so much nicer than trying to grab "RCP", "rcp", "Recipient" pages via a bajillion try/catches.
我的问题是,我可以在某种索引中获取所有电子表格页面名称,以便我可以遍历它们并尝试找到我需要的三个名称吗?这比通过大量尝试/捕获尝试获取“RCP”、“rcp”、“收件人”页面要好得多。
I'm close, because I can get the COUNT of pages in an Excel spreadsheet via the following:
我很接近,因为我可以通过以下方式获取 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
Thank you for your time.
感谢您的时间。
采纳答案by Derek
foreach ( Worksheet worksheet in excelWorkbook.Worksheets )
{
MessageBox.Show( worksheet.Name );
}
You could use a 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 );