2009-08-13 12 views
3

数値を対応するMS Excelのヘッダー値に変換するロジックを行うには、何か助けが必要です。例えば数字を対応するExcelの列に変換する

1 = "" 2 = "B" 3 = "C" 4 = "D" 5 = "E" ......... 25 = "Y" 26 = "Z" 27 = "AA" 28 = "AB" 29 = "AC" 30 = "AD" .........

このためにいくつかの.NETコード(C#またはVB)をいただければ幸いです。ありがとう。

+0

状況によっては、次のように試してみてください:http://stackoverflow.com/questions/837155/fastest-function-to-generate-excel-column-letters-in-c/837673#837673 – barrowc

+0

最高の投票回答を試してください[ここに](http://stackoverflow.com/questions/181596/how-to-convert-a-column-number-eg-127-into-an-excel-column-eg-aa)(答えが受け入れられました...) –

答えて

1

ここにいくつかのVBA(テストコード付き)がありますが、これはExcelで一緒になってトリックを行います。 VB.NETが大幅に変更されていない限り、正常に動作するはずです。たとえそれがあっても、アイディアを実行可能なコードに変換できるはずです。

' 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 
0
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(); 
} 
-1

基数変換ルーチンを使用します。 http://www.vbforums.com/showthread.php?t=271359

+1

これはBase26ではありません。 –

+0

今は混乱しています。それは何の基礎ですか?各桁のA〜Z。 26の可能性はありませんか? – TheJacobTaylor

+0

@TheJacobTaylorあなたはそのスキームに '0'がありません。 1つは '8,9,11,12、... 19,21,22 ... 'を数えません。' A'は '1'のためのものです。 Zを1つ増やすとA0(これはこのシステムにはない)がAAでなくなります。 –

0

これを試してみてください:

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]; 
} 
-1

ちょうどactivecell.addressは、文字列をmanuipulate使用あなたはのように「A」

に各桁を追加し26のベースにベース10から変換したいです$があなたの必要とする場所に基づいています。

関連する問題