使用OpenXml操作Excel,以下方法用于在添加列时修改Cell的CellReference属性。

以下方法实现了递增Excel中单元格的CellReference的功能,只支持两位字母。

 1 public static string CellReferenceIncrement(string cellReference)
 2 {
 3     Match m1 = Regex.Match(cellReference, "^([A-Z]+)");
 4     Match m2 = Regex.Match(cellReference, @"(\d+)$");
 5 
 6     string value = m1.Value;
 7     List<char> newChars = new List<char>();
 8     for (int i = value.Length - 1; i >= 0; )
 9     {
10         if (value[i] == 'Z')
11         {
12             newChars.Insert(0, 'A');
13             if ((i - 1) >= 0)
14                 newChars.Insert(0, (char)((int)value[i - 1] + 1));
15             else
16                 newChars.Insert(0, 'A');
17             break;
18         }
19         else
20         {
21             newChars.Insert(0, (char)((int)value[i] + 1));
22             if ((i - 1) >= 0)
23                 newChars.Insert(0, value[i - 1]);
24             break;
25         }
26     }
27     return string.Join("", newChars) + m2.Value;
28 }

以下为测试代码:

1 static void Main(string[] args)
2 {
3     List<string> tList = new List<string>() { "A2", "Z3", "AA3", "AZ9", "WF20" };
4     foreach (string s in tList)
5         Console.WriteLine(CellReferenceIncrement(s));
6 }

以下为输出结果:

转载于:https://www.cnblogs.com/jiao1855/p/5854856.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值