如何将Excel表格列名转换为数字? [英] How to convert Excel sheet column names into numbers?
本文介绍了如何将Excel表格列名转换为数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道什么是将excel表格列名转换成数字的最佳方法。
I was wondering what is the best way to convert excel sheet column names into numbers.
我正在使用 Excel包,一个很好的库来处理.xlsx文档。这个图书馆不幸没有包含这个功能。
I'm working with Excel Package, a good library to handle .xlsx documents. This library unfortunately doesn't have this functionality included.
OBS:第一列,A对应
到第1列
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屋!
查看全文