Excel中行列范围的转换

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

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

        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;
        }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值