• Excel中行列范围的转换


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

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

    1. public static Tupleint>, List<string>> ConvertRowColumn(string rowRep, string colRep)
    2. {
    3. List<int> rowIdxs = new List<int>();
    4. rowRep = rowRep.Replace(" ", "");
    5. colRep = colRep.Replace(" ", "");
    6. foreach (string item in rowRep.Split(','))
    7. {
    8. Match singleValM = Regex.Match(item, @"^(\d+)$");
    9. Match rangeValM = Regex.Match(item, @"^(\d+)-(\d+)$");
    10. if (singleValM.Success)
    11. {
    12. int rowIdx = int.Parse(singleValM.Groups[1].Value);
    13. rowIdxs.Add(rowIdx);
    14. }
    15. else if (rangeValM.Success)
    16. {
    17. int a = int.Parse(rangeValM.Groups[1].Value);
    18. int b = int.Parse(rangeValM.Groups[2].Value);
    19. int start = Math.Min(a, b);
    20. int end = Math.Max(a, b);
    21. for (int i = start; i <= end; i++)
    22. {
    23. rowIdxs.Add(i);
    24. }
    25. }
    26. else
    27. {
    28. //报错,不能识别
    29. }
    30. }
    31. List<int> colIdxs = new List<int>();
    32. foreach (string item in colRep.Split(','))
    33. {
    34. Match singleValM = Regex.Match(item, @"^([a-z]+)$", RegexOptions.IgnoreCase);
    35. Match rangeValM = Regex.Match(item, @"^([a-z]+)-([a-z]+)$", RegexOptions.IgnoreCase);
    36. if (singleValM.Success)
    37. {
    38. int colIdx = ColumnTitleToNumber(singleValM.Groups[1].Value);
    39. colIdxs.Add(colIdx);
    40. }
    41. else if (rangeValM.Success)
    42. {
    43. int a = ColumnTitleToNumber(rangeValM.Groups[1].Value);
    44. int b = ColumnTitleToNumber(rangeValM.Groups[2].Value);
    45. int start = Math.Min(a, b);
    46. int end = Math.Max(a, b);
    47. for (int i = start; i <= end; i++)
    48. {
    49. colIdxs.Add(i);
    50. }
    51. }
    52. else
    53. {
    54. //报错,不能识别
    55. }
    56. }
    57. rowIdxs.Sort();
    58. colIdxs.Sort();
    59. List<string> colTitles = new List<string>();
    60. foreach (int colIdx in colIdxs)
    61. {
    62. colTitles.Add(ColumnNumberToTitle(colIdx));
    63. }
    64. Tupleint>, List<string>> rowsCols = new Tupleint>, List<string>>(rowIdxs, colTitles);
    65. return rowsCols;
    66. }
    67. private static int ColumnTitleToNumber(string columnTitle)
    68. {
    69. columnTitle = columnTitle.ToUpper();
    70. int result = 0;
    71. for (int i = 0; i < columnTitle.Length; i++)
    72. {
    73. result *= 26;
    74. result += columnTitle[i] - 'A' + 1;
    75. }
    76. return result;
    77. }
    78. private static string ColumnNumberToTitle(int columnNumber)
    79. {
    80. string result = "";
    81. while (columnNumber > 0)
    82. {
    83. int remainder = (columnNumber - 1) % 26;
    84. result = (char)(remainder + 'A') + result;
    85. columnNumber = (columnNumber - 1) / 26;
    86. }
    87. return result;
    88. }

  • 相关阅读:
    Redis知识点总结-钊兵的笔记
    【重拾C语言】八、表单数据组织——结构体(类型、类型别名、直接/间接访问;典例:复数、成绩单)
    PC_浮点数加减运算
    Unity ECS 内存分配器原理详解
    【证明】若向量组线性相关,则向量组的线性映射也线性相关
    IP请求工具
    每日一练 | 网络工程师软考真题Day39
    在SQL中:如何使用命令创建、修改、添加数据库
    【AIGC】开源声音克隆GPT-SoVITS
    YOLOPose实战:手把手实现单阶段的人体姿态估计+代码解读
  • 原文地址:https://blog.csdn.net/weixin_43483847/article/details/134162780