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

谁都会走 提交于 2019-12-19 05:34:41

问题


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

回答1:


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}


来源:https://stackoverflow.com/questions/30494913/is-there-a-way-to-get-named-cells-using-epplus

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!