我正在寻找给定行/列对的单元格的名称。我需要在公式中使用的名称。例如,我想将单元格(3,4)设置为将单元格(4,4)和(5,4)的值相加的公式。
单元格(3,4)是D5
; (4,4)是E5
,(5,4)是F5
。因此,我的公式应如下所示:
=E5+F5
我可以这样格式化我的公式:
const int col = 3;
const int row = 4;
worksheet.Cells[row, col].Formula = string.Format(
"={0}{1}+{2}{3}"
, (char)('A'+col+1) // <<== Broken, do not use
, row+1
, (char)('A'+col+2) // <<== Broken, do not use
, row+1
);
这对于A..Z列工作正常,但对于名称在右侧更远的列会中断。我可以使用本问答中描述的技巧,但是问题看起来如此基本,以至于不需要我编写代码。
您无需艰难地执行此操作,因为Aspose做到了。他们的CellsHelper
班级有CellIndexToName
方法,该方法正是您所需要的:
const int col = 3;
const int row = 4;
worksheet.Cells[row, col].Formula =
$"={CellsHelper.CellIndexToName(row, col+1)}+{CellsHelper.CellIndexToName(row, col+2)}";
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句