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;
        }
相关推荐
集成显卡2 小时前
PlayWright | 初识微软出品的 WEB 应用自动化测试框架
前端·chrome·测试工具·microsoft·自动化·edge浏览器
沉到海底去吧Go3 小时前
【行驶证识别成表格】批量OCR行驶证识别与Excel自动化处理系统,行驶证扫描件和照片图片识别后保存为Excel表格,基于QT和华为ocr识别的实现教程
自动化·ocr·excel·行驶证识别·行驶证识别表格·批量行驶证读取表格
南林yan6 小时前
DLL动态库实现文件遍历功能(Windows编程)
windows
Mike_6666 小时前
win10安装WSL2、Ubuntu24.04
windows·ubuntu·wsl2
liulun7 小时前
Skia如何绘制几何图形
c++·windows
old_power7 小时前
UCRT 和 MSVC 的区别(Windows 平台上 C/C++ 开发相关)
c语言·c++·windows
扛枪的书生7 小时前
AD 提权-CVE-2022-26923: CertiFried
windows·渗透·kali·提权·域渗透
Leinwin8 小时前
行业案例 | ASOS 借助 Azure AI Foundry(国际版)为年轻时尚爱好者打造惊喜体验
人工智能·microsoft·azure
面朝大海,春不暖,花不开9 小时前
Python 文件操作与输入输出:从基础到高级应用
windows·python·microsoft
染指111010 小时前
35.x64汇编写法(二)
汇编·windows·x64游戏·x64汇编·游戏攻防