Convert Column Text Excel to Number ex: A-> 1, B->2

0 thích 0 không thích
1 lượt xem
đã hỏi 7 Tháng 10, 2023 trong Lập trình C# bởi nguyenthao (9,000 điểm)
public int ConvertCellTextToNumber(string columnName) // A => 1
{
    if (string.IsNullOrEmpty(columnName)) return -1;

    columnName = columnName.ToUpperInvariant();

    int sum = 0;

    for (int i = 0; i < columnName.Length; i++)
    {
        sum *= 26;
        sum += (columnName[i] - 'A' + 1);
    }

    return sum;
}

Looking for an answer?  Share this question:     

Xin vui lòng đăng nhập hoặc đăng ký để trả lời câu hỏi này.

...