Click here to Skip to main content
15,888,527 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
if we select an Excel sheet then how do we get the Excel Column Headers name
Posted
Updated 2-Mar-20 1:54am
Comments
Wendelius 12-Aug-11 9:39am    
What do you mean by column header names? Columns are named A, B, C... Could you elaborate more.

public static Dictionary<string,int> GetColumnMapping(Excel.Worksheet ExcelSheet, int HeaderRowIndex , List<string> Headers, Dictionary<string,int> HeaderMapping)
        {
            if (HeaderMapping != null)
                HeaderMapping.Clear();

            object misValue = System.Reflection.Missing.Value;

            Excel.Range HeaderRange = ExcelSheet.Rows[HeaderRowIndex , misValue];
            Excel.Range currentFind = null;

            foreach (string Header in Headers)
            {
                currentFind = HeaderRange.Find(Header, misValue, Excel.XlFindLookIn.xlValues, Excel.XlLookAt.xlPart, Excel.XlSearchOrder.xlByColumns, Excel.XlSearchDirection.xlNext, false, misValue, misValue);
                if(currentFind != null)
                    HeaderMapping.Add(Header, currentFind.Column);
            }

            return HeaderMapping;
        }
 
Share this answer
 
public List<String> getHeaderData(Sheet sheet) {
		List<String> headers = new ArrayList<>();
		Row row;
		int headerId = 0;
		if (sheet.getRow(headerId) != null && sheet.getRow(headerId).cellIterator() != null) {
			row = sheet.getRow(headerId);

			for (int h = 0; h <= row.getLastCellNum(); h++) {
				if (row.getCell((short) h) != null && row.getCell((short) h).toString().length() != 0) {
					headers.add(row.getCell((short) h).toString());
				}
			}

		}
		return headers;
	}
 
Share this answer
 
Comments
Richard Deeming 3-Mar-20 9:16am    
The question was tagged as C#, so an unexplained Java code-block using an unspecified library (possibly POI?) isn't really a solution to the question that was asked.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900