将 Excel 列字母转换为其数字的算法是什么?

编程入门 行业动态 更新时间:2024-10-26 08:21:34
本文介绍了将 Excel 列字母转换为其数字的算法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我需要一种算法来将 Excel Column 字母转换为其正确的数字.

I need an algorithm to convert an Excel Column letter to its proper number.

这将用 C# 编写的语言,但任何语言都可以,甚至是伪代码.

The language this will be written in is C#, but any would do or even pseudo code.

请注意,我将把它放在 C# 中,我不想使用 office dll.

Please note I am going to put this in C# and I don't want to use the office dll.

对于A",预期结果将为 1

For 'A' the expected result will be 1

对于 'AH' = 34

For 'AH' = 34

对于 'XFD' = 16384

For 'XFD' = 16384

推荐答案

public static int ExcelColumnNameToNumber(string columnName) { if (string.IsNullOrEmpty(columnName)) throw new ArgumentNullException("columnName"); columnName = columnName.ToUpperInvariant(); int sum = 0; for (int i = 0; i < columnName.Length; i++) { sum *= 26; sum += (columnName[i] - 'A' + 1); } return sum; }

更多推荐

将 Excel 列字母转换为其数字的算法是什么?

本文发布于:2023-11-30 10:00:36,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1649584.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:为其   算法   字母   数字   Excel

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!