C# 将 A 转换为 1 B 到 2 ... Z 到 26,然后将 AA 转换为 27 AB 到 28(列索引到 Excel 中的列引用)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1951517/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 22:09:20  来源:igfitidea点击:

Convert A to 1 B to 2 ... Z to 26 and then AA to 27 AB to 28 (column indexes to column references in Excel)

c#numbers

提问by Thunder

Does any one have algorithm or logic to Convert A to 1 ,B to 2, ... ,Z to 26 and then ,AA to 27, AB to 28 etc.

有没有人有算法或逻辑将 A 转换为 1 ,B 到 2,... ,Z 到 26,然后,AA 到 27,AB 到 28 等。

In other words, converting a column index into the column reference in Excel.

换句话说,将列索引转换为 Excel 中的列引用。

采纳答案by Adriaan Stander

Have a look at these

看看这些

/// <summary>
/// 1 -> A<br/>
/// 2 -> B<br/>
/// 3 -> C<br/>
/// ...
/// </summary>
/// <param name="column"></param>
/// <returns></returns>
public static string ExcelColumnFromNumber(int column)
{
    string columnString = "";
    decimal columnNumber = column;
    while (columnNumber > 0)
    {
        decimal currentLetterNumber = (columnNumber - 1) % 26;
        char currentLetter = (char)(currentLetterNumber + 65);
        columnString = currentLetter + columnString;
        columnNumber = (columnNumber - (currentLetterNumber + 1)) / 26;
    }
    return columnString;
}

/// <summary>
/// A -> 1<br/>
/// B -> 2<br/>
/// C -> 3<br/>
/// ...
/// </summary>
/// <param name="column"></param>
/// <returns></returns>
public static int NumberFromExcelColumn(string column)
{
    int retVal = 0;
    string col = column.ToUpper();
    for (int iChar = col.Length - 1; iChar >= 0; iChar--)
    {
        char colPiece = col[iChar];
        int colNum = colPiece - 64;
        retVal = retVal + colNum * (int)Math.Pow(26, col.Length - (iChar + 1));
    }
    return retVal;
}

回答by Thomas Freudenberg

Here's a simple LINQ expression:

这是一个简单的 LINQ 表达式:

static int TextToNumber(this string text) {
    return text
        .Select(c => c - 'A' + 1)
        .Aggregate((sum, next) => sum*26 + next);
}

This test

这个测试

Console.WriteLine(" A -> " + "A".TextToNumber());
Console.WriteLine(" B -> " + "B".TextToNumber());
Console.WriteLine(" Z -> " + "Z".TextToNumber());
Console.WriteLine("AA -> " + "AA".TextToNumber());
Console.WriteLine("AB -> " + "AB".TextToNumber());

will produce this output:

将产生这个输出:

 A -> 1
 B -> 2
 Z -> 26
AA -> 27
AB -> 28

Update:Here's the same code but targetting .NET 2.0:

更新:这是相同的代码,但针对 .NET 2.0:

static int TextToNumber(string text) {
    int sum = 0;
    foreach (char c in text) {
        sum = sum*26 + c - 'A' + 1;
    }
    return sum;
}

回答by Gama Sharma

This is a code for JavaScript if you prefer it done on the client side

如果您希望在客户端完成,这是 JavaScript 的代码

<script type="text/javascript" lang="javascript">
function Alphabet2Numeric(mystr) {
    mystr = mystr.toUpperCase(); //Hence the ASCII code 64 down there
    var sum = 0;
    for (var i = 0; i < mystr.length; i++) {
        sum = sum * 26 + mystr.charCodeAt(i) - 64; //returns 1 for 'a' and 2 for 'b' so on and so forth.
    }
    return sum;
}
</script>

回答by Jens A. Koch

Same problem, different language: PHP.

同样的问题,不同的语言:PHP。

function charToInt($char)
{
   $array = array_flip(range(a, z));
   return $array[$char] + 1;
}

echo charToInt('c');

outputs: 3