惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

B
Blog
量子位
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Last Week in AI
Last Week in AI
酷 壳 – CoolShell
酷 壳 – CoolShell
人人都是产品经理
人人都是产品经理
Jina AI
Jina AI
雷峰网
雷峰网
博客园_首页
WordPress大学
WordPress大学
博客园 - 司徒正美
爱范儿
爱范儿
博客园 - 聂微东
IT之家
IT之家
美团技术团队
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 三生石上(FineUI控件)
有赞技术团队
有赞技术团队
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
T
Tailwind CSS Blog
博客园 - Franky
V
V2EX
GbyAI
GbyAI
阮一峰的网络日志
阮一峰的网络日志

博客园 - bestcomy

在OPENSUE 11.3 上安装 sysstat Upload bug in MOSS 2007 / Windows Server 2008 - bestcomy Clienside ProgressIndicator - bestcomy - 博客园 ASP.NET MVC 源代码发布! 微软项目组招聘.NET开发人员(北京) 欢迎访问我的新BLOG空间 SharePoint 胡言乱语 你觉得vs.net那些地方让你使用起来很不爽? Include javascript and css dynamically - bestcomy Two ways to get HtmlTextWriter for HTML rendering How to set value for webpart’s properties when I create a new sharepoint template Get the true value of a calculate field in SharePoint 调用自定义event时需要注意的一个问题 self==top 如何设置AD用户的"用户下次登陆时须更改密码"属性 (LDAP Provider) 微软 and 香烟 Cool!!! Virtual Earth Technology Preview sth about our coding life 还没有Windows Live Messenger的朋友看过来
Excel列名转换
bestcomy · 2009-02-04 · via 博客园 - bestcomy

最近工作中发现需要转换Excel列名,例如A列序号为0,Z列序号为25,ZB列则为27
发现字母列名实际为26进制,于是写了如下Helper Class来解决我的问题:

 1 
 2         public class ExcelColumnTranslator
 3         {
 4             private ExcelColumnTranslator()
 5             { 
 6             }
 7 
 8             public static int ToIndex(string columnName)
 9             {
10                 if (!Regex.IsMatch(columnName.ToUpper(), @"[A-Z]+"))
11                     throw new Exception("invalid parameter");
12                 int index = 0;
13                 char[] chars = columnName.ToUpper().ToCharArray();
14                 for (int i = 0; i < chars.Length; i++)
15                 {
16                     index += ((int)chars[i] - (int)'A' + 1* (int)Math.Pow(26, chars.Length - i - 1);
17                 }
18                 return index - 1;
19             }
20 
21             public static string ToName(int index)
22             {
23                 if (index < 0)
24                     throw new Exception("invalid parameter");
25                 List<string> chars = new List<string>();
26                 do
27                 {
28                     if (chars.Count > 0) index--;
29                     chars.Insert(0, ((char)(index % 26 + (int)'A')).ToString());
30                     index = (int)((index - index % 26/ 26);
31                 } while (index > 0);
32                 
33                 return String.Join(string.Empty, chars.ToArray());
34             }
35         }

测试代码:

1 
2             string[] cols = new string[] { "A""AA""AAA""Z""ZZ""ZZZ""ABC""CBA""XZB" };
3             for (int i = 0; i < cols.Length; i++)
4             {
5                 Console.WriteLine("{0} == {1}", cols[i], ExcelColumnTranslator.ToName(ExcelColumnTranslator.ToIndex(cols[i])));
6             }

测试输出:
A == A
AA == AA
AAA == AAA
Z == Z
ZZ == ZZ
ZZZ == ZZZ
ABC == ABC
CBA == CBA
XZB == XZB

仅供参考