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;
        }
相关推荐
爱睡觉的圈圈18 小时前
突破反爬限制:动态IP轮换策略与实现
windows·tcp/ip·microsoft
fdc201718 小时前
Avalonia:使用附加属性实现命令与事件的绑定
javascript·windows·microsoft
float_六七19 小时前
Java Stream流:从入门到精通
java·windows·python
星空的资源小屋19 小时前
PPTist,一个完全免费的 AI 生成 PPT 在线网站
人工智能·python·电脑·excel
开开心心_Every19 小时前
免费语音合成工具:66种音色随心选
人工智能·面试·java-ee·计算机外设·电脑·maven·excel
你是人间五月天1 天前
sentinel实现控制台与nacos数据双向绑定
windows·sentinel
小猫挖掘机(绝版)1 天前
通过tailscale实现一台电脑上vscode通过ssh连接另一台电脑上的VMware Linux 虚拟机
linux·windows·vscode·ubuntu·ssh
kyle~2 天前
Qt---对话框QDialog
数据库·qt·microsoft
偷心伊普西隆2 天前
EXCEL VBA 清空Excel工作表(Sheet)的方法
microsoft·excel
你我约定有三2 天前
java--泛型
java·开发语言·windows