C#:EXCEL列名、列序号之间互相转换

EXCEL的列名与列序号 之前的关系如下

|----|----|
| A | 1 |
| B | 2 |
| C | 3 |
| D | 4 |
| E | 5 |
| F | 6 |
| G | 7 |
| H | 8 |
| I | 9 |
| J | 10 |
| K | 11 |
| L | 12 |
| M | 13 |
| N | 14 |
| O | 15 |
| P | 16 |
| Q | 17 |
| R | 18 |
| S | 19 |
| T | 20 |
| U | 21 |
| V | 22 |
| W | 23 |
| X | 24 |
| Y | 25 |
| Z | 26 |
| AA | 27 |
| AB | 28 |

cs 复制代码
        /// <summary>
        /// 根据给的EXCEL列序号,得出列名字母
        /// </summary>
        /// <param name="iColNum">序号</param>
        /// <returns>列名</returns>
        public string ColNum2Name(int iColNum)
        {
            string result = "";

            if (iColNum < 1 || iColNum > 16384)
            {
                throw new Exception("列号超出范围");
            }

            while (iColNum > 0)
            {
                iColNum--; //列号是从1开始的,字母从0开始的
                result = (char)('A' + iColNum % 26) + result;
                iColNum /= 26;
            }
            return result;
        }

        /// <summary>
        /// 根据给出的EXCEL列名,转换成列序号
        /// </summary>
        /// <param name="sColName">列名</param>
        /// <returns>序号</returns>
        public int ColName2Num(string sColName)
        {
            int result = 0;
            for (int i = 0; i < sColName.Length; i++)
            {
                result *= 26;
                result += sColName[i] - 'A' + 1;
            }
            return result;
        }
相关推荐
Dxy12393102163 小时前
Elasticsearch 聚合入门:像 Excel 透视表一样分析数据
elasticsearch·excel
一位代码4 小时前
Excel | .xls 与 .xlsx 文件的区别及使用 Excel 打开 CSV 文件出现乱码的解决办法
excel
CodeCraft Studio4 小时前
Excel处理控件Aspose.Cells教程:使用C#在Excel中创建树状图
前端·c#·excel·aspose·c# excel库·excel树状图·excel sdk
悟能不能悟6 小时前
vue导出excel文件
前端·vue.js·excel
CodeCraft Studio6 小时前
国产化Excel开发组件Spire.XLS教程:以Python编程方式在Excel中高亮重复值
开发语言·python·excel·spire.xls·excel自动化·excel高亮重复值·python处理excel
喝汽水的猫^20 小时前
Java实现Excel 导出(多 Sheet、复杂格式)
java·excel
小费的部落1 天前
Excel 在Sheet3中 匹配Sheet1的A列和Sheet2的A列并处理空内容
java·前端·excel
缺点内向1 天前
如何在 C# 中重命名 Excel 工作表并设置标签颜色
开发语言·c#·excel
金融小师妹1 天前
AI算法视角下非农夜冲击波来袭,黄金高位区间震荡态势的深度神经网络解析
大数据·深度学习·1024程序员节
wtsolutions1 天前
Sheet-to-Doc: Automate Document Generation with Excel Data and Word Templates
word·excel·wtsolutions