在Aspose Cells中获取单元名称

问题描述 投票:1回答:1

我希望获得给定行/列对的单元格的名称。我需要在公式中使用名称。例如,我想将单元格(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,但问题看起来很基本,不应该要求我编码。

c# .net aspose
1个回答
2
投票

你不需要这么做,因为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)}";
© www.soinside.com 2019 - 2024. All rights reserved.