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;
        }
相关推荐
YCOSA20253 小时前
雨晨 Windows 10 企业版 LTSC x64 中度精简 1809 迎双节 17763.9257 特别版
windows
sukalot4 小时前
windows 驱动实例分析系列: HidHide 驱动分析 - drivers 篇(四)
windows·驱动开发
E-iceblue7 小时前
Excel 列转行/行列转换全指南:从 4 种常见解法到 Python 批量自动化
python·excel·python库·spire.xls
ι:9 小时前
NUC12 Pro 安装 Ubuntu 24.04 LTS 详细教程
linux·windows·ubuntu·nuc
YCOSA20259 小时前
微软宣布将于10月7日举行Windows与Surface发布会,聚焦“Windows、NVIDIA RTX Spark、Surface,以及更广泛PC生态”
windows
Source.Liu10 小时前
【A11】 labelprinter(Tauri 版)新建步骤
windows·rust
Punchline27811 小时前
解决 Windows 环境变量 Path 设置时提示“字符太长”的完整指南
windows·环境变量·path
chushiyunen11 小时前
claude code笔记(二)、claude桌面端(内网不好用)
windows
d_benhua12 小时前
以太网测试步骤
windows
宝桥南山12 小时前
Microsoft Fabric - 简单尝试一下Microsoft Fabric .NET SDK
microsoft·微软·.net·.netcore·powerbi·fabric