如何在 .NET 中按名称获取工作表?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8067115/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 16:01:03  来源:igfitidea点击:

How to get a worksheet by name in .NET?

.netexcel

提问by Arnthor

Being forced from NPOI to microsoft interop, I have to perform a task of finding a certain worksheet in workbook, and then iterate through every row of it.

被迫从 NPOI 到 microsoft interop,我必须执行在工作簿中查找某个工作表的任务,然后遍历它的每一行。

In NPOI it would be simply workbook.GetSheet(sheetName);. What would be the equivalent for this in microsoft interop?

在 NPOI 中,它很简单workbook.GetSheet(sheetName);。在微软互操作中,这相当于什么?

回答by Matt

Use workbook.Sheets[sheetName];

workbook.Sheets[sheetName];

Complete working example:

完整的工作示例:

using Microsoft.Office.Interop.Excel;

class Program
{
    static void Main(string[] args)
    {
        var excelApplication = new Application();
        excelApplication.Visible = true;
        excelApplication.SheetsInNewWorkbook = 3;
        var workbook = excelApplication.Workbooks.Add();
        var worksheet = workbook.Sheets["Sheet2"];         //<--- desired method
        worksheet.Activate();
    }
}