C# EXCEL操作中数字和字符串(列)转换

//C#代码字符串转数字
int StringToNumber(string s)
{
int r = 0 ;
for (int i = 0; i < s.Length; i++)
{
r = r * 26 + s[i] - ‘A’ + 1;
}
return r;
}

//C#代码数字转字符串
static string NumbertoString(int n)
{
string s = “” ;
int r = 0 ;
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 ;
}

//C++代码字符串转数字
int StringToNumber(string s)
{
int r = 0 ;
for (unsigned i = 0; i < s.length(); i++)
{
r = r * 26 + s[i] - ‘A’ + 1;
}
return r ;
}

//C++代码数字转字符串
string NumbertoString(int n)
{
string s = “” ;
int r = 0 ;
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 ;
}

你可能感兴趣的:(C#)