Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public string Letter(int intCol) {
- int intFirstLetter = ((intCol) / 676) + 64;
- int intSecondLetter = ((intCol % 676) / 26) + 64;
- int intThirdLetter = (intCol % 26) + 65;
- char FirstLetter = (intFirstLetter > 64) ? (char)intFirstLetter : ' ';
- char SecondLetter = (intSecondLetter > 64) ? (char)intSecondLetter : ' ';
- char ThirdLetter = (char)intThirdLetter;
- return string.Concat(FirstLetter, SecondLetter, ThirdLetter).Trim();
- }
- private string[] map = new string[]
- {
- "A", "B", "C", "D", "E" .............
- };
- public string getColumn(int number)
- {
- return map[number];
- }
- Application.Worksheets("Sheet1").Range("B1").Font.Bold = True
- Application.Worksheets("Sheet1").Cells(1, 2).Font.Bold = True
- public static string ExcelColumnName(int count)
- {
- return new string((char)('A'+(((count-1)%26))), ((count-1) / 26) + 1);
- }
- public class Test
- {
- private Dictionary<int, string> m_Cache = new Dictionary<int, string>();
- public string ExcelColumnName(int count)
- {
- if (m_Cache.ContainsKey(count) == false)
- {
- m_Cache.Add(count, new string((char)('A' + (((count - 1) % 26))), ((count - 1) / 26) + 1));
- }
- return m_Cache[count];
- }
- public static void Main(string[] args)
- {
- Test t = new Test();
- Console.WriteLine(t.ExcelColumnName(1));
- Console.WriteLine(t.ExcelColumnName(26));
- Console.WriteLine(t.ExcelColumnName(27));
- Console.WriteLine(t.ExcelColumnName(1));
- Console.WriteLine(t.ExcelColumnName(26));
- Console.WriteLine(t.ExcelColumnName(27));
- }
- public static string ExcelColumnFromNumber(int column)
- {
- string columnString = "";
- decimal columnNumber = column;
- while (columnNumber > 0)
- {
- decimal currentLetterNumber = (columnNumber - 1) % 26;
- char currentLetter = (char)(currentLetterNumber + 65);
- columnString = currentLetter + columnString;
- columnNumber = (columnNumber - (currentLetterNumber + 1)) / 26;
- }
- return columnString;
- }
- public static int NumberFromExcelColumn(string column)
- {
- int retVal = 0;
- string col = column.ToUpper();
- for (int iChar = col.Length - 1; iChar >= 0; iChar--)
- {
- char colPiece = col[iChar];
- int colNum = colPiece - 64;
- retVal = retVal + colNum * (int)Math.Pow(26, col.Length - (iChar + 1));
- }
- return retVal;
- }
- // Returns name of column for specified 0-based index.
- public static string GetColumnName(int index)
- {
- var name = new char[3]; // Assumes 3-letter column name max.
- int rem = index;
- int div = 17576; // 26 ^ 3
- for (int i = 2; i >= 0; i++)
- {
- name[i] = alphabet[rem / div];
- rem %= div;
- div /= 26;
- }
- if (index >= 676)
- return new string(name, 3);
- else if (index >= 26)
- return new string(name, 2);
- else
- return new string(name, 1);
- }
- static Dictionary<int, string> LetterDict = new Dictionary<int, string>(676);
- public static string LetterWithCaching(int index)
- {
- int intCol = index - 1;
- if (LetterDict.ContainsKey(intCol)) return LetterDict[intCol];
- int intFirstLetter = ((intCol) / 676) + 64;
- int intSecondLetter = ((intCol % 676) / 26) + 64;
- int intThirdLetter = (intCol % 26) + 65;
- char FirstLetter = (intFirstLetter > 64) ? (char)intFirstLetter : ' ';
- char SecondLetter = (intSecondLetter > 64) ? (char)intSecondLetter : ' ';
- char ThirdLetter = (char)intThirdLetter;
- String s = string.Concat(FirstLetter, SecondLetter, ThirdLetter).Trim();
- LetterDict.Add(intCol, s);
- return s;
- }
- class ToolSheet
- {
- //Not the prettyest but surely the fastest :
- static string[] ColName = new string[676];
- public ToolSheet()
- {
- ColName[0] = "A";
- for (int index = 1; index < 676; ++index) Recurse(index, index);
- }
- private int Recurse(int i, int index)
- {
- if (i < 1) return 0;
- ColName[index] = ((char)(65 + i % 26)).ToString() + ColName[index];
- return Recurse(i / 26, index);
- }
- public string GetColName(int i)
- {
- return ColName[i - 1];
- }
- }
- class ToolSheet
- {
- //Not the prettyest but surely the fastest :
- static string[] ColName = new string[676];
- public ToolSheet()
- {
- for (int index = 0; index < 676; ++index)
- {
- Recurse(index, index);
- }
- }
- private int Recurse(int i, int index)
- {
- if (i < 1)
- {
- if (index % 26 == 0 && index > 0) ColName[index] = ColName[index - 1].Substring(0, ColName[index - 1].Length - 1) + "Z";
- return 0;
- }
- ColName[index] = ((char)(64 + i % 26)).ToString() + ColName[index];
- return Recurse(i / 26, index);
- }
- public string GetColName(int i)
- {
- return ColName[i - 1];
- }
- }
- public string Letter(int intCol) {
- int intFirstLetter = ((intCol) / 676) + 64;
- int intSecondLetter = ((intCol % 676) / 26) + 64;
- int intThirdLetter = (intCol % 26) + 65; ' SHOULD BE + 64?
- char FirstLetter = (intFirstLetter > 64) ? (char)intFirstLetter : ' ';
- char SecondLetter = (intSecondLetter > 64) ? (char)intSecondLetter : ' ';
- char ThirdLetter = (char)intThirdLetter;
- return string.Concat(FirstLetter, SecondLetter, ThirdLetter).Trim();
- }
Add Comment
Please, Sign In to add comment