Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to get 'named' cells using EPPlus?

I have an Excel file that I am populating programmatically with EPPlus.

I have tried the following:

// provides access to named ranges, does not appear to work with single cells
worksheet.Names["namedCell1"].Value = "abc123";

// provides access to cells by address
worksheet.Cells["namedCell1"].Value = "abc123";

The following does work - so I know I am at least close.

worksheet.Cells["A1"].Value = "abc123";
like image 258
gnobes Avatar asked May 27 '15 23:05

gnobes


People also ask

How do you navigate to named cells?

You can find a named range by going to the Home tab, clicking Find & Select, and then Go To. Or, press Ctrl+G on your keyboard. In the Go to box, double-click the named range you want to find.

Does EPPlus require Excel?

No, it does not require Excel to be installed on the server, as you can read in the docs: EPPlus is a . NET library that reads and writes Excel files using the Office Open XML format (xlsx).


1 Answers

Actually, its a bit misleading. The Named Ranges are stored at the workBOOK level and not the workSHEET level. So if you do something like this:

[TestMethod]
public void Get_Named_Range_Test()
{
    //http://stackoverflow.com/questions/30494913/is-there-a-way-to-get-named-cells-using-epplus

    var existingFile = new FileInfo(@"c:\temp\NamedRange.xlsx");
    using (var pck = new ExcelPackage(existingFile))
    {
        var wb = pck.Workbook; //Not workSHEET
        var namedCell1 = wb.Names["namedCell1"];

        Console.WriteLine("{{\"before\": {0}}}", namedCell1.Value);
        namedCell1.Value = "abc123";
        Console.WriteLine("{{\"after\": {0}}}", namedCell1.Value);
    }
}

You get this in the output (using an excel file with dummy data in it):

{"before": Range1 B2}
{"after": abc123}
like image 118
Ernie S Avatar answered Sep 24 '22 19:09

Ernie S