有没有办法获得命名'细胞使用EPPlus?

时间:2015-05-27 23:39:33

标签: epplus epplus-4

我有一个Excel文件,我用EPPlus以编程方式填充。

我尝试了以下内容:

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

以下工作 - 所以我知道我至少接近。

worksheet.Cells["A1"].Value = "abc123";

1 个答案:

答案 0 :(得分:12)

实际上,它有点误导。命名范围存储在workBOOK级别而不是workSHEET级别。所以如果你做这样的事情:

[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);
    }
}

你在输出中得到这个(使用带有虚拟数据的excel文件):

{"before": Range1 B2}
{"after": abc123}