简体   繁体   English

Excel / C#-通过名称获取工作表ID?

[英]Excel/C# - Get worksheet id by name?

I currently have a worksheet with several sheets : 我目前有一个包含几张纸的工作表:
Sheet_Alpha | Sheet_Alpha | Sheet 2 | 第2页| Sheet_Beta | Sheet_Beta | Sheet 4 | 第4页| Sheet 5 | 第5页| Sheet_Delta Sheet_Delta

How I can recover them id according to their name? 如何根据他们的名字找回他们的身份证? To be then able to select the sheet which I want with : Worksheets.get_Item() 为了能够选择我想要的Worksheets.get_Item()Worksheets.get_Item()

Is it possible? 可能吗? I looked for a moment and I find nothing to do it. 我看了片刻,却无所适从。

Thanks :) 谢谢 :)

You can first transfer info about all the sheets of the workbook to a List as below 您可以首先将有关工作簿所有工作表的信息转移到如下列表中

string name = "xxx";
List<Worksheet> lstSheet = new List<Worksheet>();
foreach (Worksheet ws in MyBook.Sheets)
{
    lstSheet.Add(ws);
}

And then access the target sheet by name as below 然后按以下名称访问目标表

Worksheet wsToQuery = lstSheet.Find(a => a.Name.Contains(name));

Hope it helps... 希望能帮助到你...

See my answer at: Get all worksheet names in plaintext from Excel with C# Interop? 在以下位置查看我的答案: 使用C#Interop从Excel中以纯文本格式获取所有工作表名称?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM