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;
        }
相关推荐
cspttty10 小时前
2027采购岗求职:大数据管理与应用专业如何把Excel、SQL变成业务能力
excel
ms365copilot13 小时前
Excel Copilot 一键定位修改的内容
excel·copilot
帆张芳显1 天前
前端表格公式能力新选择|智表 ZCell 公式引擎深度剖析
前端·excel·canva可画·excel插件
地图小课堂1 天前
Bigemap Pro 导入 Excel、CSV、TXT 点位表格教程
excel
我的愿望是成为富婆2 天前
2027校招准备:从Excel操作到数据分析,还缺业务、SQL与统计思维
sql·数据分析·excel
用户0332126663672 天前
Python 自动化办公:如何从零快速生成结构清晰的 Excel 报表
python·excel
千桐科技2 天前
qData 数据中台专业版功能解析:告别 Excel 与 SQL 拼凑,打通系统合并的可追踪数据链路
数据库·sql·excel·qdata
百事牛科技2 天前
从局部到整体:Excel限制功能的几种实用设置
windows·excel
ProcessOn官方账号3 天前
ProcessOn Office 插件上线:在 Word / Excel / PPT 里直接画流程图
word·excel·插件·office·processon
reasonsummer4 天前
【办公类-53-12】20261016(预测)Python模仿制作2026学年第二学期校历(excel+pdf,上下学期都包含)
pdf·excel