C# 获得Excel的列标
public static string GetColumnName(int theColumnIndex)
{
string tmpColName = string.Empty;
string tmpColName1=string.Empty;
if (theColumnIndex > 256) { return ""; }
else {
if (theColumnIndex <= 26) { tmpColName = Chr(Asc("A") + theColumnIndex - 1); }
else
{
tmpColName1 = Chr(Asc("A") + (theColumnIndex / 26) - 1);
tmpColName = tmpColName1 + Chr(Asc("A") + theColumnIndex % 26 - 1);
}
}
return tmpColName;
}
public static int Asc(string character)
{
if (character.Length == 1)
{
System.Text.ASCIIEncoding asciiEncoding = new System.Text.ASCIIEncoding();
int intAsciiCode = (int)asciiEncoding.GetBytes(character)[0];
return (intAsciiCode);
}
else
{
throw new Exception("Character is not valid.");
}
}
public static string Chr(int asciiCode)
{
if (asciiCode >= 0 && asciiCode <= 255)
{
System.Text.ASCIIEncoding asciiEncoding = new System.Text.ASCIIEncoding();
byte[] byteArray = new byte[] { (byte)asciiCode };
string strCharacter = asciiEncoding.GetString(byteArray);
return (strCharacter);
}
else
{
throw new Exception("ASCII Code is not valid.");
}
}