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;
        }
相关推荐
wtsolutions3 小时前
JSON to Excel Add-in - Seamless Integration Within Excel
json·excel
wtsolutions3 小时前
Getting Started with JSON to Excel Web App - Convert in Seconds
json·excel·web app
wtsolutions7 小时前
Using the JSON to Excel API - Programmatic Access for Developers
json·excel
qq_435139577 小时前
EasyExcel(FastExcel)Excel导出功能 技术文档
excel
wtsolutions10 小时前
Understanding JSON Formats - What JSON to Excel Supports
json·excel
wtsolutions10 小时前
Advanced Features - Unlocking the Power of JSON to Excel Pro
linux·json·excel
fs哆哆12 小时前
VB.NET和VBA教程-如何查找Excel数据区域边界
excel
小矮强12 小时前
Excel中根据年月日提取月日,并按月日进行排序
excel
开开心心就好12 小时前
内存清理工具开源免费,自动优化清理项
linux·运维·服务器·python·django·pdf·1024程序员节
开开心心_Every13 小时前
图片批量压缩工具:支持有损无损两种模式
python·游戏·微信·django·pdf·excel·语音识别