c# - 如何按名称设置 Excel 单元格的值而不是使用 EPPlus 的坐标?

标签 c# excel openxml epplus excelpackage

我可以使用 EPPlus 设置单元格值:

var template = new FileInfo(Server.MapPath("~/App_Data/my_template.xlsx"));
var pck = new ExcelPackage(template);
var ws = pck.Workbook.Worksheets.First();
ws.Cells[15, 4].Value = 66; 
ws.Cells["B1"].Value = 55;

我不想通过坐标定位目标单元格 使用命名单元格/变量“利润”。

ws.Range("profits").Value = 66;

但是,EPPlus 不支持 ws.Range(...)。

=> 是否有可能这样做?我将如何编写提供所需功能的扩展方法?

相关文章/问题:

a) EPPlus 文档:

http://epplus.codeplex.com

b) 如何在 Excel 中使用命名单元格/变量:

https://www.computerhope.com/issues/ch000704.htm

What's the RIGHT way to reference named cells in Excel 2013 VBA? (I know I'm messing this up)

c) 使用 C# 创建 Excel 文件的库

Create Excel (.XLS and .XLSX) file from C#

最佳答案

var profits = pck.Workbook.Names["profits"];
profits.Value = 66;

来自

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

也可以为命名范围设置值:

var ws = pck.Workbook.Worksheets.First();
using (var namedRange = pck.Workbook.Names["MyNamedRange"])
      {
        for (int rowIndex = namedRange.Start.Row; rowIndex <= namedRange.End.Row; rowIndex++)
        {
          for (int columnIndex = namedRange.Start.Column; columnIndex <= namedRange.End.Column; columnIndex++)
          {
            ws.Cells[rowIndex, columnIndex].Value = 66;
          }
        }
      }

来自

EPPlus, Find and set the value for a Named Range

关于c# - 如何按名称设置 Excel 单元格的值而不是使用 EPPlus 的坐标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43910827/

相关文章:

c# - TabControl 中的缩进选项卡

c# - AutoMapper 和 is*Specified 属性

Excel:100% 堆栈条形图,对于少数值组合,条形图绘制不正确

excel - 将 VLOOKUP 与列并集结合使用

c# - 如何在给定接口(interface)的情况下创建对象?

c# - WCF 跟踪和消息日志记录不会在服务主机(服务器)端写入日志文件

python - 使用 Python 动态地将值写入 Excel 中的单元格

c# - Excel 表验证/公式未复制到新表行

c# - 使用 C# 和 OpenXML 在 xlsx 文件中写入时,Excel 单元格文本被修剪

c# - 更改 OpenXML word 文档中的字体颜色 (C#)