/// <summary>
/// 获取Excel实际列索引
/// </summary>
/// <param name="columnName">Excel列名</param>
/// <returns>int格式索引</returns>
public int GetColumnIndex(string columnName)
{
	int result = 0;
	// A-Z 转换成 0-25的数字,并反转
	int[] colReverse = Encoding.ASCII.GetBytes(columnName.ToUpper()).Select(x =>
	{
		int aIndex = Encoding.ASCII.GetBytes("A")[0];
		int zIndex = Encoding.ASCII.GetBytes("Z")[0];
		return x < aIndex || x > zIndex ? 
            throw new ArgumentException($"参数有误{nameof(columnName)}") : 
        		x - aIndex;
	}).Reverse().ToArray();

    // 当成26进制数,遍历计算
    for (int i = 0; i < colReverse.Count(); i++)
    {
        // 个位(0~25)  其他位(1~26)
        int vReal = colReverse[i] + (i == 0 ? 0 : 1);
        result += vReal * (int)Math.Pow(26, i);
    }
    return result;
}

相关文章:

  • 2022-01-07
  • 2021-11-02
  • 2022-12-23
  • 2021-08-18
  • 2022-12-23
  • 2021-08-27
  • 2022-02-15
  • 2021-12-15
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-12-04
  • 2022-03-07
  • 2021-12-31
  • 2021-12-06
相关资源
相似解决方案