zl程序教程

您现在的位置是:首页 >  其他

当前栏目

获取Excel列标

2023-02-19 12:17:09 时间
/// <summary>
/// 获取Excel列标 A B C...Z等
/// </summary>
/// <param name="col">Excel列数,从0开始</param>
/// <returns></returns>
public string getExcelColumnLabel(int col)
{
    string temp = "";
    double i = Math.Floor(Math.Log(25.0 * (col) / 26.0 + 1) / Math.Log(26)) + 1;
    if (i > 1)
    {
        double sub = col - 26 * (Math.Pow(26, i - 1) - 1) / 25;
        for (double j = i; j > 0; j--)
        {
            temp = temp + (char)(sub / Math.Pow(26, j - 1) + 65);
            sub = sub % Math.Pow(26, j - 1);
        }
    }
    else
    {
        temp = temp + (char)(col + 65);
    }
    return temp;
}