字母和数字的转换——Excel列名

来源:互联网 发布:网络信息化领导小组 编辑:程序博客网 时间:2024/05/30 05:02

 在用代码操作Excel的过程中(如OpenXml),会用到把列名转化为数字,然后再进行计算确认列处理。

   把列名转化为数字很容易实现定位。下面分享的这两个方法的主要作用是:

    (1)把字母转为数字, 如1转为A,AA转为27 ,然后进行处理;

    (2)把数字转为字母,A->1,27->AA……(这个比较常用)。

1、字母转数字

    思想: 从字符串的最后一位到第一位,乘以26的幂,依次相加

  算法: 26^0 * (最后一位 ) + 26 ^ 1 * (前一位 ) + …… + 26 ^ n * (第一位)。

复制代码
 1         private int MoreCharToInt(string value)
2 {
3 int rtn = 0;
4 int powIndex = 0;
5
6 for (int i = value.Length - 1; i >= 0; i--)
7 {
8 int tmpInt = value[i];
9 tmpInt -= 64;
10
11 rtn += (int)Math.Pow(26, powIndex) * tmpInt;
12 powIndex++;
13 }
14
15 return rtn;
16 }
复制代码

2、数字转为字母

    思想: 字母对应的数字的算法为:26^0 * A + 26 ^ 1 * A ……,

      按照这个规律 每次除以26,就可以得到每一位的值,然后进行转换。

      但是有个小问题,就是如果这一位是字符 ‘Z’ 的话,就会进位,转换完后,处理进位的值即可(这里是关键哦)。

复制代码
 1         private string IntToMoreChar(int value)
2 {
3 string rtn = string.Empty;
4 List<int> iList = new List<int>();
5
6 //To single Int
7 while (value / 26 != 0 || value % 26 != 0)
8 {
9 iList.Add(value % 26);
10 value /= 26;
11 }
12
13 //Change 0 To 26
14 for (int j = 0; j < iList.Count - 1; j++)
15 {
16 if (iList[j] == 0)
17 {
18 iList[j + 1] -= 1;
19 iList[j] = 26;
20 }
21 }
22
23 //Remove 0 at last
24 if (iList[iList.Count - 1] == 0)
25 {
26 iList.Remove(iList[iList.Count - 1]);
27 }
28
29 //To String
30 for (int j = iList.Count - 1; j >= 0; j--)
31 {
32 char c = (char)(iList[j] + 64);
33 rtn += c.ToString();
34 }
35
36 return rtn;
37 }
复制代码

   

  小弟不才, 花了一段时间才想出来的,希望对大家能有所帮助吧!

  以后对于 功能、方法的算法,我会尽量分享出来,供大家讨论,以获取更好的思路。

分类: C#点滴
绿色通道: 好文要顶 关注我 收藏该文与我联系 
Shoubin
关注 - 16
粉丝 - 8
+加关注
0
0
(请您对文章做出评价)
» 博主后一篇:Insert的一个小应用——复制数据
posted @ 2011-07-29 17:45 Shoubin 阅读(2643) 评论(3) 编辑 收藏

  回复引用
#1楼 2011-07-30 11:22 | zdd  
以前也研究过这个问题,把我的C++版贴出来。
?
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
29
30
31
32
33
34
35
36
// Convert A, B, .... AAA, AAB to number 1, 2, ...
int StringToNumber(string s)
{
    int r = 0 ;
    for (unsigned i = 0; i < s.length(); i++)
    {
        r = r * 26 + s[i] - 'A' + 1;
    }
    return r ;
}
 
// Convert number 1, 2, ... to string A, B, ...
string NumbertoString(int n)
{          
    string s = "" ;    // result
    int r = 0 ;         // remainder
 
    while(n)
    {
        r = n % 26 ;
 
        char ch = ' ' ;
        if(r == 0)
            ch = 'Z' ;
        else
            ch = r - 1 + 'A' ;
        s.insert(0, 1, ch) ;
 
        if(s[0] == 'Z')
            n = n / 26 - 1 ;
        else
            n /= 26 ;
    }
 
    return s ;
}
支持(0)反对(0)
  回复引用
#2楼 2011-07-30 12:35 | 秋色  
还以为就我一个人在仿excel呢,原来这么多人。
支持(0)反对(0)
  回复引用
#3楼[楼主2011-08-12 12:14 | Shoubin  
@zdd
非常感谢你的分享,
你用的算法思想以及执行效率都比我的高啊,极力推荐
我用C#语言稍微修改了一下,希望你不要介意。
代码如下:
?
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
29
30
31
32
33
34
// Convert A, B, .... AAA, AAB to number 1,2, ...        
int StringToNumber(string s) 
        
            int r = 0 ; 
            for (int i = 0; i < s.Length; i++) 
            
                r = r * 26 + s[i] - 'A' + 1; 
            
            return r ; 
        
 
// Convert number 1, 2, ... to string A, B, ... 
        static string NumbertoString(int n) 
 
         {            
             string s = "" ;    // result 
             int r = 0 ;         // remainder 
 
             while(n != 0) 
             
                 r = n % 26 ; 
                 char ch = ' ' 
                 if(r == 0) 
                     ch = 'Z' 
                 else
                    ch = (char)(r - 1 + 'A') ; 
                    s = ch.ToString() + s;
                 if(s[0] == 'Z'
                     n = n / 26 - 1 ; 
                 else 
                     n /= 26 ; 
             
             return s ; 
         }