本文主要是介绍C#将数字转正字母的方法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
将列号(数字)转换成Excel的字母列名
如
1→A
28→AB
实现:
private string getCellCode(int inCell)
{string strRet = "A";if (inCell >= 1 && inCell <= 256){
<span style="white-space:pre"> </span>if (inCell <= 26){
<span style="white-space:pre"> </span> Byte[] bytes = new Byte[] { (byte)(inCell + 64) };ASCIIEncoding ascii = new ASCIIEncoding();strRet = ascii.GetString(bytes);}else{
<span style="white-space:pre"> </span> int int1 = 0;int int2 = 0;
<span style="white-space:pre"> </span> if (inCell - (int)(inCell / 26) * 26 == 0)
<span style="white-space:pre"> </span> {
<span style="white-space:pre"> </span>int1 = (int)(inCell / 26) + 63;
<span style="white-space:pre"> </span>int2 = 90;}<span style="white-space:pre"> </span> else<span style="white-space:pre"> </span> {<span style="white-space:pre"> </span>int1 = (int)(inCell / 26) + 64;<span style="white-space:pre"> </span>int2 = (inCell - (int)(inCell / 26) * 26) + 64;}Byte[] bytes = new Byte[] { (byte)int1, (byte)int2 };ASCIIEncoding ascii = new ASCIIEncoding();strRet = ascii.GetString(bytes);<span style="white-space:pre"> </span>}}return strRet;
}
这篇关于C#将数字转正字母的方法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!