Las siguientes funciones complementarias obtienen el numero de columna en Excel basado en el nombre y viceversa.
Ej
GetExcelColumnName(731) ==> "ABC"
GetExcelColumnNumber("AB") ==> 28
/// <summary>
/// Get the a Excel Column based on a column number
/// </summary>
/// <param name="ColumnNumber">Column Number</param>
/// <returns></returns>
public static string GetExcelColumnName(int ColumnNumber)
{
int intDividend = ColumnNumber;
string strColumnName = String.Empty;
int intModulo;
while (intDividend > 0)
{
intModulo = (intDividend - 1) % 26;
strColumnName = Convert.ToChar(65 + intModulo).ToString() + strColumnName;
intDividend = (int)((intDividend - intModulo) / 26);
}
return strColumnName;
}
/// <summary>
/// Get the Excel column number based on a excel column name
/// </summary>
/// <param name="ColumnName">Nombre de la columna</param>
/// <returns></returns>
public static int GetExcelColumnNumber(string ColumnName)
{
int columnNumber=0;
for (int i=0;i<ColumnName.Length ;i++){
columnNumber =
columnNumber * 26 +(Convert.ToInt32 (ColumnName [i]) - 64)
;
}
return columnNumber;
}