我有一个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";
实际上,这有点误导。命名范围存储在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:tempNamedRange.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}