Excel 列序数转列字母表达
1 function printString(n){ 2 let arr = []; 3 let i = 0; 4 5 // Step 1: Converting to number assuming 6 // 0 in number system 7 while (n) { 8 arr[i] = n % 26; 9 n = Math.floor(n / 26); 10 i++; 11 } 12 13 // Step 2: Getting rid of 0, as 0 is 14 // not part of number system 15 for (let j = 0; j < i - 1; j++) { 16 if (arr[j] <= 0) { 17 arr[j] += 26; 18 arr[j + 1] = arr[j + 1] - 1; 19 } 20 } 21 let ans = ''; 22 for (let j = i; j >= 0; j--) { 23 if (arr[j] > 0) 24 ans += String.fromCharCode(65 + arr[j] - 1); 25 } 26 27 document.write(ans + "<br>"); 28 } 29
printString(26);//Z
printString(51);//
AYprintString(100);//
CV C#实现:
1 static string ColumnIndexToColumnLetter(int colIndex) 2 { 3 int div = colIndex; 4 string colLetter = String.Empty; 5 int mod = 0; 6 7 while (div > 0) 8 { 9 mod = (div - 1) % 26; 10 colLetter = (char)(65 + mod) + colLetter; 11 div = (int)((div - mod) / 26); 12 } 13 return colLetter; 14 }
string columnLetter = ColumnIndexToColumnLetter(100); // returns CV