我正在使用 EPPLus 打开 Excel 电子表格并尝试从命名范围中读取。命名范围为空。

是我用错了还是EPPlus有问题

代码

        var package = new ExcelPackage();

        using (var stream = File.OpenRead(tmpExcel))
        {
            package.Load(stream);
        }

        var worksheet = package.Workbook.Worksheets["Common-Lookup"];

        using (ExcelNamedRange namedRange = worksheet.Names["LupVerticalSettings"])
        {
            for (var row = namedRange.Start.Row; row <= namedRange.End.Row; row++)
            {
                for (var col = namedRange.Start.Column; col <= namedRange.End.Column; col++)
                {
                    _.Nv(worksheet.Cells[row, col].Address, worksheet.Cells[row, col].Text);
                    //worksheet.Cells[rowIndex, columnIndex].Value = "no more hair pulling";
                }
            }
        }

Excel 看起来像这样

空命名范围

最佳答案

我解决了我的问题,我会在这里为将来可能需要它的任何人提供答案

    var package = new ExcelPackage();

    using (var stream = File.OpenRead(tmpExcel))
    {
        package.Load(stream);
    }

    var worksheet = package.Workbook.Worksheets["Common-Lookup"];

    // Access Named Ranges from the ExcelWorkbook instead of ExcelWorksheet
    //using (ExcelNamedRange namedRange = worksheet.Names["LupVerticalSettings"])

    // use package.Workbook.Names instead of worksheet.Names
    using (ExcelNamedRange namedRange = package.Workbook.Names["LupVerticalSettings"])
    {
        for (var row = namedRange.Start.Row; row <= namedRange.End.Row; row++)
        {
            for (var col = namedRange.Start.Column; col <= namedRange.End.Column; col++)
            {
                _.Nv(worksheet.Cells[row, col].Address, worksheet.Cells[row, col].Text);
                //worksheet.Cells[rowIndex, columnIndex].Value = "no more hair pulling";
            }
        }
    }

10-05 21:18