如何找到对应于给定的整数Excel的列名?整数、应于、Excel

2023-09-11 01:47:52 作者:小镇与凉梦

你如何确定列名的第n列(如阿Q或BH)在Excel中?

How would you determine the column name (e.g. "AQ" or "BH") of the nth column in Excel?

编辑:语言无关的算法来确定这是主要目标,在这里

A language-agnostic algorithm to determine this is the main goal here.

推荐答案

我曾经写过这个函数来执行具体任务:

I once wrote this function to perform that exact task:

public static string Column(int column)
{
    column--;
    if (column >= 0 && column < 26)
        return ((char)('A' + column)).ToString();
    else if (column > 25)
        return Column(column / 26) + Column(column % 26 + 1);
    else
        throw new Exception("Invalid Column #" + (column + 1).ToString());
}