How to get a worksheet by name in .NET?

后端 未结 1 1269
别跟我提以往
别跟我提以往 2021-01-01 17:41

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.

In NPOI i

相关标签:
1条回答
  • 2021-01-01 18:14

    Use 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();
        }
    }
    
    0 讨论(0)
提交回复
热议问题