如何Excel工作表的列名转换成数字? [英] How to convert Excel sheet column names into numbers?

查看:286
本文介绍了如何Excel工作表的列名转换成数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道什么是转换Excel工作表的列名成数字的最佳方式。



我用的 Excel中包,一个好的图书馆处理的.xlsx文件。 。这个库遗憾的是不包括此功能。




OBS:第一列A,相当于
到1号在这个库。



解决方案

这个功能应该工作的一个任意长度的列名。

 公共静态INT GetColumnNumber(字符串名称)
{
INT数= 0;
INT POW = 1;
的for(int i = name.Length - 1; I> = 0;我 - )
{
号+ =(名称[I] - 'A'+ 1)*战俘;
POW * = 26;
}

返回数;
}


I was wondering what is the best way to convert excel sheet column names into numbers.

I'm working with Excel Package, a good library to handle .xlsx documents. This library unfortunately doesn't have this functionality included.

OBS: The first column, A, corresponds to number 1 in this library.

解决方案

This function should work for an arbitrary length column name.

public static int GetColumnNumber(string name)
{
    int number = 0;
    int pow = 1;
    for (int i = name.Length - 1; i >= 0; i--)
    {
        number += (name[i] - 'A' + 1) * pow;
        pow *= 26;
    }

    return number;
}

这篇关于如何Excel工作表的列名转换成数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆