获取Aspose Cells中的单元格名称

Obtaining cell name in Aspose Cells

我希望获得给定 row/column 对的单元格的名称。我需要一个在公式中使用的名称。例如,我想将单元格 (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 列工作正常,但对于名称更靠右的列会中断。我可以使用 a trick described in this Q&A,但问题看起来很简单,我不需要编码。

您无需费力地完成此操作,因为 Aspose 已为您完成。他们的 CellsHelper class 有 CellIndexToName 方法,这正是你需要的:

const int col = 3;
const int row = 4;
worksheet.Cells[row, col].Formula =
    $"={CellsHelper.CellIndexToName(row, col+1)}+{CellsHelper.CellIndexToName(row, col+2)}";