2020年2月6日 星期四

[C#] 英文轉數字

一般的英文轉數字

int a = (int)"A";


用於Excel將英文欄位轉換成對應的數值

      private int Column(string str)
        {
            char[] arr = str.ToCharArray();
            int column = 0;

            if (arr.Length == 1)
            {
                column = (int)arr[0] - 65 + 1;
            }
            else if (arr.Length == 2)
            {
                column = ((int)arr[0] - 65 + 1) * 26 + (int)arr[1] - 65 + 1;
            }
           
            return column;
        }