有没有办法使用EPPlus获得“命名"单元格? [英] Is there a way to get 'named' cells using EPPlus?
本文介绍了有没有办法使用EPPlus获得“命名"单元格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个Excel文件,该文件是用EPPlus编程填充的.
I have an Excel file that I am populating programmatically with 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";
以下内容确实有效-所以我知道我至少很亲近.
The following does work - so I know I am at least close.
worksheet.Cells["A1"].Value = "abc123";
推荐答案
实际上,这有点误导.命名范围存储在workBOOK
级别而不是workSHEET
级别.因此,如果您执行以下操作:
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);
}
}
您在输出中得到此信息(使用其中包含虚拟数据的excel文件):
You get this in the output (using an excel file with dummy data in it):
{"before": Range1 B2}
{"after": abc123}
这篇关于有没有办法使用EPPlus获得“命名"单元格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文