• 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. }

  • 相关阅读:
    TiDB数据迁移工具概览
    metaRTC5.0编程之p2p网络穿透(stun)指南
    Metasploit——后渗透测试阶段
    什么是MIMO?
    3.DApp-Metamask登录不了解决方法
    经典算法-----农夫过河问题(深度优先搜索)
    linux内核进程间通信IPC----消息队列
    【无标题】Open Verification Library Assertion检查
    新轮子 Caddy 入手体验
    如何快速实现一个可视化看板?
  • 原文地址:https://blog.csdn.net/weixin_43483847/article/details/134162780