【问题标题】:Convert Number to Corresponding Excel Column [duplicate]将数字转换为相应的 Excel 列 [重复]
【发布时间】:2010-11-19 04:28:13
【问题描述】:

我需要一些帮助来执行将数值转换为相应的 MS Excel 标头值的逻辑。

例如:

1 = “A” 2 =“乙” 3 = "C" 4 = “D” 5 = “E” ………… 25 =“是” 26 = "Z" 27 = "AA" 28 = "AB" 29 = "交流" 30 = "广告" .........

希望为此提供一些 .NET 代码(C# 或 VB)。谢谢。

【问题讨论】:

标签: excel vsto numbers letter


【解决方案1】:

这是我在 Excel 中串在一起的一些 VBA(带有测试代码),可以解决问题。除非 VB.NET 发生了巨大的变化,否则它应该可以正常工作。即使有,您也应该能够将 idea 转化为可行的代码。

' num2col - translate Excel column number (1-n) into column string ("A"-"ZZ"). '

Function num2col(num As Integer) As String
    ' Subtract one to make modulo/divide cleaner. '

    num = num - 1

    ' Select return value based on invalid/one-char/two-char input. '

    If num < 0 Or num >= 27 * 26 Then
        ' Return special sentinel value if out of range. '

        num2col = "-"
    Else
        ' Single char, just get the letter. '

        If num < 26 Then
            num2col = Chr(num + 65)
        Else
           ' Double char, get letters based on integer divide and modulus. '

           num2col = Chr(num \ 26 + 64) + Chr(num Mod 26 + 65)
        End If
    End If
End Function

 

' Test code in Excel VBA. '

Sub main()
    MsgBox ("-  should be " & num2col(0))
    MsgBox ("A  should be " & num2col(1))
    MsgBox ("B  should be " & num2col(2))
    MsgBox ("Z  should be " & num2col(26))
    MsgBox ("AA should be " & num2col(27))
    MsgBox ("AB should be " & num2col(28))
    MsgBox ("AY should be " & num2col(51))
    MsgBox ("AZ should be " & num2col(52))
    MsgBox ("BA should be " & num2col(53))
    MsgBox ("ZY should be " & num2col(27 * 26 - 1))
    MsgBox ("ZZ should be " & num2col(27 * 26))
    MsgBox ("-  should be " & num2col(27 * 26 + 1))
End Sub

【讨论】:

    【解决方案2】:
    public string ColumnNumberToLetter(int ColumnNumber)
    {
        if (ColumnNumber > 26)
        {
            return ((char) (Math.Floor(((double)ColumnNumber - 1) / 26) + 64)).ToString()
                   + ((char) (((ColumnNumber - 1) % 26) + 65)).ToString();
        }
        return ((char)(ColumnNumber+64)).ToString();
    }
    

    【讨论】:

      【解决方案3】:

      试试这个:

      public static string ToExcelString(int number)
      {
          if (number > 25)
          {
              int secondaryCounter = 0;
              while (number > 25)
              {
                   secondaryCounter = secondaryCounter + 1;
                   number = number - 25;
              }
              return ToExcelChar(number) + ToExcelChar(secondaryCounter);
          }
          else
          {
              return ToExcelChar(number)
          }
      }
      private const string alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
      private static string ToExcelChar(int number)
      {
          if (number > 25 || number < 0)
          {
              throw new InvalidArgumentException("the number passed in (" + number + ") must be between the range 0-25");
          }
          return alphabet[number];
      }
      

      【讨论】:

        【解决方案4】:

        使用数基转换例程。您想从基数 10 转换为基数 26。将每个数字添加到“A”

        如:http://www.vbforums.com/showthread.php?t=271359

        【讨论】:

        • 它不是Base26,你会偏离一点。
        • 现在感到很困惑。它是什么基础?每个数字的 A-Z。那不是26种可能性吗?
        • @TheJacobTaylor 因为您在该方案中缺少0。一个不算8, 9, 11, 12, ... 19, 21, 22 ... 请记住A 代表1。 Z 增加 1 应该给你 A0(不在这个系统中),而不是 AA。
        【解决方案5】:

        只需使用 activecell.address,然后根据 $ 所在位置对字符串进行操作即可。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2011-03-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多