Retrieve column index through column header Names

Hi,

Is there any way where i can retrieve the column index based on column header names in a worksheet?

@DivyanshuSrivastava31,

We have utility class named CellsHelper, so you may try to use relevant static method (ColumnNameToIndex) for it. See the following sample code to accomplish your task for your reference.

// get index of D column
int destTxtCol = CellsHelper.ColumnNameToIndex("D"); 

Hope, this helps a bit.

Hi Amjad,

If a cell consist of some string value and based on the cell value i want to retrieve the column index, then what would be the right approach?

image.png (37.8 KB)

For instance, in the attached image if the cell have value “Header”, how would i get the column index

@DivyanshuSrivastava31,

You may simply use Find/Search options provided by Aspose.Cells for your task. See the following sample code for your reference.
e.g.
Sample code:

// Instantiate the workbook object
Workbook workbook = new Workbook("book1.xls");

// Get Cells collection
Cells cells = workbook.Worksheets[0].Cells;

FindOptions opts = new FindOptions();
opts.LookInType = LookInType.Values;
opts.LookAtType = LookAtType.EntireContent;

// Find the cell with the input string
Aspose.Cells.Cell cell = cells.Find("Header", null, opts);

//Get column and row indexes
int colIndex = cell.Column;
int rowIndex = cell.Row;

Also, see the document on Find or Search Data for your reference.

Thanks amjad,

i got the info which i needed

@DivyanshuSrivastava31,

Good to know that the suggested code segment/detail helps you with your needs. Please feel free to write back to us if you have further queries or comments.