我正在尝试使用vb.net将数据写入excel文件 . 所以我的函数将数字列转换为excel字母列 .
Public Function ConvertToLetter(ByRef iCol As Integer) As String
Dim Reminder_Part As Integer = iCol Mod 26
Dim Integer_Part As Integer = Int(iCol / 26)
If Integer_Part = 0 Then
ConvertToLetter = Chr(Reminder_Part + 64)
ElseIf Integer_Part > 0 And Reminder_Part <> 0 Then
ConvertToLetter = Chr(Integer_Part + 64) + Chr(Reminder_Part + 64)
ElseIf Integer_Part > 0 And Reminder_Part = 0 Then
ConvertToLetter = Chr(Integer_Part * 26 + 64)
End If
End Function
该功能适用于任何其他数字 .
例如,
-
1 => A.
-
2 => B.
-
......
-
26 => Z.
-
27 => AA
-
......
-
51 => AY
-
52 => t(这是它开始出错的时候)假设返回AZ,但它返回了t .
我无法弄清楚我犯了什么错误 . 有人可以帮我或者告诉我如何编写使用vb.net将数字转换为excel字母列的正确功能 .
3 回答
这应该做你想要的 .
这将最多工作52 .
另外,您可以通过.Net直接使用EPPlus编写XLSX文件 . 如果您愿意,可以使用字母表示法,或者您可以使用数字 .
逻辑中存在一些缺陷,第二个else子句不是必需的,操作应该基于零 .