如何从文本引用获取Worksheet对象?

发布于 2024-11-27 01:23:10 字数 74 浏览 0 评论 0原文

我在不同的工作簿中有一个工作表名称。名称如下所示:“[Book.xlsx]Sheet1”。如何从该名称获取 Worksheet 对象?

I have a name of worksheet in a different workbook. Name looks like this: '[Book.xlsx]Sheet1'. How I can get Worksheet object from this name?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

贵在坚持 2024-12-04 01:23:10

试试这个:

    /// <summary>
    /// Get a worksheet by a uniqueSheetIdentifierString like "[WorkbookName]worksheetName".
    /// </summary>
    /// <param name="uniqueSheetIdentifierString"></param>
    /// <returns>A worksheet</returns>
    public Worksheet GetWorksheetByUniqueSheetIdentifier(string uniqueSheetIdentifierString)
    {
        const char OPENING_SQUARE_BRACKET = '[';
        const char CLOSING_SQUARE_BRACKET = ']';

        //Argument checks
        if (string.IsNullOrEmpty(uniqueSheetIdentifierString)) throw new ArgumentNullException("The uniqueSheetIdentifierString must have the format '[WorkbookName]SheetName', but was empty!");
        if (!uniqueSheetIdentifierString.StartsWith(OPENING_SQUARE_BRACKET.ToString())) throw new ArgumentException("The uniqueSheetIdentifierString must have the format '[WorkbookName]SheetName', but the opening square bracket could not be found: " + uniqueSheetIdentifierString);

        //Try getting position of closing square bracket...
        var indexOfClosingSquareBracket = uniqueSheetIdentifierString.IndexOf(CLOSING_SQUARE_BRACKET);
        if (indexOfClosingSquareBracket < 2) throw new ArgumentException("The uniqueSheetIdentifierString must have the format '[WorkbookName]SheetName', but the closing square bracket could not be found or is not at a valid position: " + uniqueSheetIdentifierString);

        //Extract workbook name and worksheet name out of a string like "[WorkbookName]worksheetName"
        var workbookName = uniqueSheetIdentifierString.Substring(1, indexOfClosingSquareBracket - 1);
        var worksheetName = uniqueSheetIdentifierString.Substring(indexOfClosingSquareBracket + 1);

        //Return the worksheet
        return Globals.ThisAddIn.Application.Workbooks[workbookName].Worksheets[worksheetName];
    }

Try this:

    /// <summary>
    /// Get a worksheet by a uniqueSheetIdentifierString like "[WorkbookName]worksheetName".
    /// </summary>
    /// <param name="uniqueSheetIdentifierString"></param>
    /// <returns>A worksheet</returns>
    public Worksheet GetWorksheetByUniqueSheetIdentifier(string uniqueSheetIdentifierString)
    {
        const char OPENING_SQUARE_BRACKET = '[';
        const char CLOSING_SQUARE_BRACKET = ']';

        //Argument checks
        if (string.IsNullOrEmpty(uniqueSheetIdentifierString)) throw new ArgumentNullException("The uniqueSheetIdentifierString must have the format '[WorkbookName]SheetName', but was empty!");
        if (!uniqueSheetIdentifierString.StartsWith(OPENING_SQUARE_BRACKET.ToString())) throw new ArgumentException("The uniqueSheetIdentifierString must have the format '[WorkbookName]SheetName', but the opening square bracket could not be found: " + uniqueSheetIdentifierString);

        //Try getting position of closing square bracket...
        var indexOfClosingSquareBracket = uniqueSheetIdentifierString.IndexOf(CLOSING_SQUARE_BRACKET);
        if (indexOfClosingSquareBracket < 2) throw new ArgumentException("The uniqueSheetIdentifierString must have the format '[WorkbookName]SheetName', but the closing square bracket could not be found or is not at a valid position: " + uniqueSheetIdentifierString);

        //Extract workbook name and worksheet name out of a string like "[WorkbookName]worksheetName"
        var workbookName = uniqueSheetIdentifierString.Substring(1, indexOfClosingSquareBracket - 1);
        var worksheetName = uniqueSheetIdentifierString.Substring(indexOfClosingSquareBracket + 1);

        //Return the worksheet
        return Globals.ThisAddIn.Application.Workbooks[workbookName].Worksheets[worksheetName];
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文