Get Column Name using Column Header

Hi,

I am looking to get the Column Name “A” when I find the column using Column Header. When I do asterickCell.Name I get the output as “A1”. Is it possible to get just the Column name A instead of Cell “A1”?

FindOptions findOptions = new FindOptions();
findOptions.CaseSensitive = false;
findOptions.LookInType = LookInType.Values;
findOptions.LookAtType = LookAtType.Contains;

Cell asterickCell = workbook.Worksheets[0].Cells.Find("Test", null, findOptions);

@santhoshsas you have the Column property but that return to you the column index and not the column name.
The Name of a cell is composed by the column letter/s and the row number, so you can transform your result to get only the letters, for example using the following code:

// Use regular expression pattern to match only letters (alphabetic characters)
string pattern = @"[^a-zA-Z]+"; // Match anything that is not an uppercase or lowercase letter
string replacement = string.Empty; // Replace non-letters with empty string

// Use Regex.Replace method to replace non-letters with empty string
string colLetter = Regex.Replace(asterickCell.Name, pattern, replacement);

@santhoshsas
Please get the column name of the cell with the following codes:

   Cell asterickCell = workbook.Worksheets[0].Cells.Find("Test", null, findOptions);
            Console.WriteLine(CellsHelper.ColumnIndexToName(asterickCell.Column));
···
1 Like

Thank You , it works.

@santhoshsas,
Thank you for your feedback. If you have any questions, please feel free to contact us.