Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EPPlus - Named Range is not populated

Tags:

c#

excel

epplus

I'm using EPPLus to open Excel Spreadsheets and trying to read from a named range. The Named Range is Empty.

Am I using this wrong, or is it a problem with EPPlus

Code

        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";
                }
            }
        }

The Excel looks like this

Excel Named Range

Empty Named Range

Empty Named Range

like image 765
David Cruwys Avatar asked Dec 06 '22 03:12

David Cruwys


1 Answers

I solved my problem, I'll put answer here for anyone that may need it in the future

    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";
            }
        }
    }
like image 146
David Cruwys Avatar answered Dec 10 '22 13:12

David Cruwys