如何使用VSTO在Excel中获取工作表的代号 [英] How to get the CodeName for Worksheet in Excel using VSTO

查看:106
本文介绍了如何使用VSTO在Excel中获取工作表的代号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我确实是这样的:

if (Excel._Application.ActiveWorkbook != null)
{
    List<WorksheetKeyValue> sheets = new List<WorksheetKeyValue>();
    foreach (object ws in ExcelApp.ActiveWorkbook.Worksheets)
    {
        string strCodeName = ws.CodeName
    }
}

但是strCodeName像VBA中那样应该为Sheet1,Sheet2,...,SheetN时为空字符串.

but strCodeName is an empty string when it supposed to be Sheet1, Sheet2, ..., SheetN like in VBA.

谢谢

推荐答案

在您的情况下,可以使用Worksheet.CustomProperties作为替代来保存工作表的唯一属性.

In your condition, you can use Worksheet.CustomProperties as alternative to hold unique property of the sheet.

Worksheet ws = **current_sheet** as Worksheet;
ws.CustomProperties.Add("SheetID", **some_value**);

因此,以后您可以通过

foreach (Excel.CustomProperty prop in ws.CustomProperties)
{
    if (prop.Name == "SheetID")
    {
       // access as prop.Value and prop.Name
    }
 }

希望这会有所帮助.

这篇关于如何使用VSTO在Excel中获取工作表的代号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆