Excel 의 Column 문자를 숫자로, 숫자를 알파벳 문자로 변환하는 방법입니다.


  1.  
  2.         /// <summary>
  3.         /// Coulumn 문자를 Int 값으로 변환
  4.         /// </summary>
  5.         /// <param name="value">Column 문자</param>
  6.         /// <returns></returns>
  7.         static public int Column2Int(string value)
  8.         {
  9.             int result = 0;
  10.  
  11.             if (value != null && value.Length > 0)
  12.             {
  13.                 foreach (char item in value)
  14.                 {
  15.                     result = 26 * result + (item - 'A');
  16.                 }
  17.  
  18.                 result++; //1 based
  19.  
  20.                 if (value.Length > 1)
  21.                 {
  22.                     result += 26;
  23.                 }
  24.             }
  25.  
  26.             return result;
  27.         }
  28.  
  29.         /// <summary>
  30.         /// Column 숫자를 알파벳 문자로 변환.
  31.         /// </summary>
  32.         /// <param name="value">Column 숫자</param>
  33.         /// <returns></returns>
  34.         static public string Int2Column(int value)
  35.         {
  36.             List<char> result = new List<char>();
  37.  
  38.             if (value > 0)
  39.             {
  40.                 value--; //1 based
  41.  
  42.                 if (value < 26)
  43.                 {
  44.                     result.Add((char)('A' + value));
  45.                 }
  46.                 else
  47.                 {
  48.                     value -= 26;
  49.                     do
  50.                     {
  51.                         result.Add((char)('A' + value % 26));
  52.                         value = value / 26;
  53.                     } while (value > 0);
  54.  
  55.                     if (result.Count == 1)
  56.                     {
  57.                         result.Add('A');
  58.                     }
  59.                 }
  60.             }
  61.  
  62.             return new string(result.Reverse<char>().ToArray());
  63.         }

행복한 고수되십시오.



woojja ))*

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\


반응형

+ Recent posts