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;
        }
相关推荐
百数平台7 小时前
百数表格知识库配置指南:Excel/CSV 上传、表结构字段类型与 NL2SQL 查询全说明
excel
葡萄城技术团队8 小时前
在 DSH 中直接查看和编辑 Excel:dsh-spreadjs-editor 开源插件发布
excel
开开心心就好9 小时前
视频里的图片怎么提取?双击一下就导出
java·前端·人工智能·spring·智能手机·intellij-idea·excel
葡萄城技术团队11 小时前
从 AI 生成到在线预览:dsh-spreadjs-editor 补齐 DSH 的 Excel 任务闭环
excel
泡海椒11 小时前
JQuick-Excel 实战:用 STYLE 配置单元格与区域样式
开发语言·python·excel
优选资讯21 小时前
标签打印软件怎么对接 Excel 数据
前端·excel
泡海椒1 天前
JQuick-Excel 实战:FORMAT 管理日期与数字的 Excel 显示
开发语言·python·excel
AIGS0011 天前
从814张表到250个本体:本体建模在做什么
数据库·excel·erp·数据中台·智能问数·本体语义·企业大脑
泡海椒1 天前
JQuick-Excel VALIDATION 实战:用作用范围约束 Excel 导入校验
开发语言·python·excel
jason.zeng@15022071 天前
(八)现有架构上新增一个通用Excel导出工具
python·架构·langchain·excel·llama