随笔 - 127  文章 - 0  评论 - 1  阅读 - 40606

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);//AY
printString(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
 
 
posted on   wakaka_wka  阅读(102)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek “源神”启动!「GitHub 热点速览」
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 我与微信审核的“相爱相杀”看个人小程序副业
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· DeepSeek R1 简明指南:架构、训练、本地部署及硬件要求
< 2025年2月 >
26 27 28 29 30 31 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 1
2 3 4 5 6 7 8

点击右上角即可分享
微信分享提示