Excel中行列范围的转换

将 行:1,4-5,8,11 列:a,c-e,f

这种写法转换成单元格地址的方法。

cs 复制代码
        public static Tuple<List<int>, List<string>> ConvertRowColumn(string rowRep, string colRep)
        {
            List<int> rowIdxs = new List<int>();

            rowRep = rowRep.Replace(" ", "");
            colRep = colRep.Replace(" ", "");

            foreach (string item in rowRep.Split(','))
            {
                Match singleValM = Regex.Match(item, @"^(\d+)$");
                Match rangeValM = Regex.Match(item, @"^(\d+)-(\d+)$");
                if (singleValM.Success)
                {
                    int rowIdx = int.Parse(singleValM.Groups[1].Value);
                    rowIdxs.Add(rowIdx);
                }
                else if (rangeValM.Success)
                {
                    int a = int.Parse(rangeValM.Groups[1].Value);
                    int b = int.Parse(rangeValM.Groups[2].Value);

                    int start = Math.Min(a, b);
                    int end = Math.Max(a, b);

                    for (int i = start; i <= end; i++)
                    {
                        rowIdxs.Add(i);
                    }
                }
                else
                {
                    //报错,不能识别
                }
            }

            List<int> colIdxs = new List<int>();
            foreach (string item in colRep.Split(','))
            {
                Match singleValM = Regex.Match(item, @"^([a-z]+)$", RegexOptions.IgnoreCase);
                Match rangeValM = Regex.Match(item, @"^([a-z]+)-([a-z]+)$", RegexOptions.IgnoreCase);
                if (singleValM.Success)
                {
                    int colIdx = ColumnTitleToNumber(singleValM.Groups[1].Value);
                    colIdxs.Add(colIdx);
                }
                else if (rangeValM.Success)
                {
                    int a = ColumnTitleToNumber(rangeValM.Groups[1].Value);
                    int b = ColumnTitleToNumber(rangeValM.Groups[2].Value);

                    int start = Math.Min(a, b);
                    int end = Math.Max(a, b);

                    for (int i = start; i <= end; i++)
                    {
                        colIdxs.Add(i);
                    }
                }
                else
                {
                    //报错,不能识别
                }
            }
            rowIdxs.Sort();
            colIdxs.Sort();


            List<string> colTitles = new List<string>();
            foreach (int colIdx in colIdxs)
            {
                colTitles.Add(ColumnNumberToTitle(colIdx));
            }

            Tuple<List<int>, List<string>> rowsCols = new Tuple<List<int>, List<string>>(rowIdxs, colTitles);
            return rowsCols;
        }

        private static int ColumnTitleToNumber(string columnTitle)
        {
            columnTitle = columnTitle.ToUpper();
            int result = 0;
            for (int i = 0; i < columnTitle.Length; i++)
            {
                result *= 26;
                result += columnTitle[i] - 'A' + 1;
            }
            return result;
        }

        private static string ColumnNumberToTitle(int columnNumber)
        {
            string result = "";
            while (columnNumber > 0)
            {
                int remainder = (columnNumber - 1) % 26;
                result = (char)(remainder + 'A') + result;
                columnNumber = (columnNumber - 1) / 26;
            }
            return result;
        }
相关推荐
IT空门:门主14 分钟前
本地的ip实现https访问-OpenSSL安装+ssl正式的生成(Windows 系统)
windows·https·ssl
安装虚拟机的老师傅15 分钟前
【2025最新】Windows系统装VSCode搭建C/C++开发环境(附带所有安装包)
c语言·windows·vscode·其他
琉璃℡初雪1 小时前
vue2/3 中使用 @vue-office/docx 在网页中预览(docx、excel、pdf)文件
vue.js·pdf·excel
越甲八千4 小时前
windowsC++操作ADB
c++·windows·adb
九班长5 小时前
Mirror的多人连接管理及房间系统
windows
一个懒鬼5 小时前
Edge浏览器打开PDF文件显示空白(每次需要等上一会)
windows·pdf
com未来6 小时前
使用 NSSM 安装 Tomcat 11.0.6 为 Windows 服务
java·windows·tomcat
不要数手指啦7 小时前
Apifox使用方法
windows
Robot25111 小时前
「华为」人形机器人赛道投资首秀!
大数据·人工智能·科技·microsoft·华为·机器人
IT专业服务商17 小时前
联想 SR550 服务器,配置 RAID 5教程!
运维·服务器·windows·microsoft·硬件架构