c# - 在 C# 中将数字转换为字母以在 Microsoft Excel 中使用

标签 c# algorithm excel

<分区>

Possible Duplicate:
How to convert a column number (eg. 127) into an excel column (eg. AA)

好的,所以我正在编写一个接受二维数组作为参数的方法。我想将这个二维数组放到 Excel 工作表上,但我需要计算出最终的单元格位置。我可以像这样轻松获得高度:

var height = data.GetLength(1); //`data` is the name of my 2d array

这为我提供了 Y 轴,但要获得我的 X 轴并不容易。显然我可以这样得到号码:

var width = data.GetLength(0);

这给了我一个数字,我想把它转换成一个字母。因此,例如,0 是 A,1 是 B,依此类推,直到我们到达 26,然后再次回到 A。我确信有一种简单的方法可以做到这一点,但我完全有心理障碍。

你会怎么做?

谢谢

最佳答案

这是一个也处理双字母列(在 Z 列之后)的版本:

static string GetColumnName(int index)
{
    const string letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

    var value = "";

    if (index >= letters.Length)
        value += letters[index / letters.Length - 1];

    value += letters[index % letters.Length];

    return value;
}

关于c# - 在 C# 中将数字转换为字母以在 Microsoft Excel 中使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10373561/

相关文章:

c# - 为什么 CLS 要求抛出/捕获异常派生对象?

Java:数组上的广度优先遍历顺序

algorithm - 嵌套 For 循环的运行时间

algorithm - F# - 算法和字符串

excel - VBA循环工作表: Delete rows if cell doesn't contain

c# - 我怎样才能让我的 trie 更有效率?

c# - 如何用 C# 设计多实例应用程序?

sql-server - Excel 工作表提示输入 SQl 登录凭据

excel - 合并两个查询而不在 Power Query 中重复行

c# - 在 WP8.1 通用应用程序中使用 PhoneAccentBrush