Guest User

Untitled

a guest
Jun 17th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.51 KB | None | 0 0
  1. public string Letter(int intCol) {
  2.  
  3. int intFirstLetter = ((intCol) / 676) + 64;
  4. int intSecondLetter = ((intCol % 676) / 26) + 64;
  5. int intThirdLetter = (intCol % 26) + 65;
  6.  
  7. char FirstLetter = (intFirstLetter > 64) ? (char)intFirstLetter : ' ';
  8. char SecondLetter = (intSecondLetter > 64) ? (char)intSecondLetter : ' ';
  9. char ThirdLetter = (char)intThirdLetter;
  10.  
  11. return string.Concat(FirstLetter, SecondLetter, ThirdLetter).Trim();
  12. }
  13.  
  14. private string[] map = new string[]
  15. {
  16. "A", "B", "C", "D", "E" .............
  17. };
  18.  
  19. public string getColumn(int number)
  20. {
  21. return map[number];
  22. }
  23.  
  24. Application.Worksheets("Sheet1").Range("B1").Font.Bold = True
  25.  
  26. Application.Worksheets("Sheet1").Cells(1, 2).Font.Bold = True
  27.  
  28. public static string ExcelColumnName(int count)
  29. {
  30. return new string((char)('A'+(((count-1)%26))), ((count-1) / 26) + 1);
  31. }
  32.  
  33. public class Test
  34. {
  35. private Dictionary<int, string> m_Cache = new Dictionary<int, string>();
  36.  
  37. public string ExcelColumnName(int count)
  38. {
  39. if (m_Cache.ContainsKey(count) == false)
  40. {
  41. m_Cache.Add(count, new string((char)('A' + (((count - 1) % 26))), ((count - 1) / 26) + 1));
  42.  
  43. }
  44.  
  45. return m_Cache[count];
  46. }
  47.  
  48. public static void Main(string[] args)
  49. {
  50. Test t = new Test();
  51.  
  52. Console.WriteLine(t.ExcelColumnName(1));
  53. Console.WriteLine(t.ExcelColumnName(26));
  54. Console.WriteLine(t.ExcelColumnName(27));
  55. Console.WriteLine(t.ExcelColumnName(1));
  56. Console.WriteLine(t.ExcelColumnName(26));
  57. Console.WriteLine(t.ExcelColumnName(27));
  58.  
  59. }
  60.  
  61. public static string ExcelColumnFromNumber(int column)
  62. {
  63. string columnString = "";
  64. decimal columnNumber = column;
  65. while (columnNumber > 0)
  66. {
  67. decimal currentLetterNumber = (columnNumber - 1) % 26;
  68. char currentLetter = (char)(currentLetterNumber + 65);
  69. columnString = currentLetter + columnString;
  70. columnNumber = (columnNumber - (currentLetterNumber + 1)) / 26;
  71. }
  72. return columnString;
  73. }
  74.  
  75. public static int NumberFromExcelColumn(string column)
  76. {
  77. int retVal = 0;
  78. string col = column.ToUpper();
  79. for (int iChar = col.Length - 1; iChar >= 0; iChar--)
  80. {
  81. char colPiece = col[iChar];
  82. int colNum = colPiece - 64;
  83. retVal = retVal + colNum * (int)Math.Pow(26, col.Length - (iChar + 1));
  84. }
  85. return retVal;
  86. }
  87.  
  88. // Returns name of column for specified 0-based index.
  89. public static string GetColumnName(int index)
  90. {
  91. var name = new char[3]; // Assumes 3-letter column name max.
  92. int rem = index;
  93. int div = 17576; // 26 ^ 3
  94.  
  95. for (int i = 2; i >= 0; i++)
  96. {
  97. name[i] = alphabet[rem / div];
  98. rem %= div;
  99. div /= 26;
  100. }
  101.  
  102. if (index >= 676)
  103. return new string(name, 3);
  104. else if (index >= 26)
  105. return new string(name, 2);
  106. else
  107. return new string(name, 1);
  108. }
  109.  
  110. static Dictionary<int, string> LetterDict = new Dictionary<int, string>(676);
  111. public static string LetterWithCaching(int index)
  112. {
  113. int intCol = index - 1;
  114. if (LetterDict.ContainsKey(intCol)) return LetterDict[intCol];
  115. int intFirstLetter = ((intCol) / 676) + 64;
  116. int intSecondLetter = ((intCol % 676) / 26) + 64;
  117. int intThirdLetter = (intCol % 26) + 65;
  118. char FirstLetter = (intFirstLetter > 64) ? (char)intFirstLetter : ' ';
  119. char SecondLetter = (intSecondLetter > 64) ? (char)intSecondLetter : ' ';
  120. char ThirdLetter = (char)intThirdLetter;
  121. String s = string.Concat(FirstLetter, SecondLetter, ThirdLetter).Trim();
  122. LetterDict.Add(intCol, s);
  123. return s;
  124. }
  125.  
  126. class ToolSheet
  127. {
  128.  
  129.  
  130. //Not the prettyest but surely the fastest :
  131. static string[] ColName = new string[676];
  132.  
  133.  
  134. public ToolSheet()
  135. {
  136. ColName[0] = "A";
  137. for (int index = 1; index < 676; ++index) Recurse(index, index);
  138.  
  139. }
  140.  
  141. private int Recurse(int i, int index)
  142. {
  143. if (i < 1) return 0;
  144. ColName[index] = ((char)(65 + i % 26)).ToString() + ColName[index];
  145.  
  146. return Recurse(i / 26, index);
  147. }
  148.  
  149. public string GetColName(int i)
  150. {
  151. return ColName[i - 1];
  152. }
  153.  
  154.  
  155.  
  156. }
  157.  
  158. class ToolSheet
  159. {
  160.  
  161.  
  162. //Not the prettyest but surely the fastest :
  163. static string[] ColName = new string[676];
  164.  
  165.  
  166. public ToolSheet()
  167. {
  168.  
  169. for (int index = 0; index < 676; ++index)
  170. {
  171. Recurse(index, index);
  172. }
  173.  
  174. }
  175.  
  176. private int Recurse(int i, int index)
  177. {
  178. if (i < 1)
  179. {
  180. if (index % 26 == 0 && index > 0) ColName[index] = ColName[index - 1].Substring(0, ColName[index - 1].Length - 1) + "Z";
  181.  
  182. return 0;
  183. }
  184.  
  185.  
  186. ColName[index] = ((char)(64 + i % 26)).ToString() + ColName[index];
  187.  
  188.  
  189. return Recurse(i / 26, index);
  190. }
  191.  
  192. public string GetColName(int i)
  193. {
  194. return ColName[i - 1];
  195. }
  196.  
  197.  
  198.  
  199. }
  200.  
  201. public string Letter(int intCol) {
  202.  
  203. int intFirstLetter = ((intCol) / 676) + 64;
  204. int intSecondLetter = ((intCol % 676) / 26) + 64;
  205. int intThirdLetter = (intCol % 26) + 65; ' SHOULD BE + 64?
  206.  
  207. char FirstLetter = (intFirstLetter > 64) ? (char)intFirstLetter : ' ';
  208. char SecondLetter = (intSecondLetter > 64) ? (char)intSecondLetter : ' ';
  209. char ThirdLetter = (char)intThirdLetter;
  210.  
  211. return string.Concat(FirstLetter, SecondLetter, ThirdLetter).Trim();
  212. }
Add Comment
Please, Sign In to add comment