0
votes

How to Read Excel file(xlsx) using Headers Apache POI , Spring Boot !!!.I know we can read using row iterator and cell iterator. I want to read using the header.

This is how I read xlsx file using row iterator and cell iterator

    InputStream ExcelFileToRead = new FileInputStream("myfile.xlsx");
    XSSFWorkbook wb = new XSSFWorkbook(ExcelFileToRead);

    XSSFWorkbook test = new XSSFWorkbook();

    XSSFSheet sheet = wb.getSheetAt(0);
    XSSFRow row;
    XSSFCell cell;


    Iterator rows = sheet.rowIterator();

    while (rows.hasNext())
    {
        row = (XSSFRow) rows.next();
        Iterator cells = row.cellIterator();

        while (cells.hasNext())
        {

            cell = (XSSFCell) cells.next();

            if (cell.getCellType() == XSSFCell.CELL_TYPE_STRING) {

                 System.out.print(cell.getStringCellValue() + " ");
            } else if (cell.getCellType() == XSSFCell.CELL_TYPE_NUMERIC) {

                DecimalFormat decimalFormat = new DecimalFormat("#0.00");

                Double impactMultiple = cell.getNumericCellValue();

                System.out.print(impactMultiple+" ");

            }
        }
        System.out.println();
    }

But How to read using Header ??

code:

Image for Reference 1:

Screen Shot

Image for Reference 2:

Screen Shot

1
Only read the first row, if that's the one with the header in?Gagravarr
For example: use sheet.getRow(0).getCell(0).getStringCellValue() to read the first column header.LHCHIN
This won't work in my case as Headers values will change their order i.e header columns are dynamic please see image for referenceMyjay1516

1 Answers

0
votes

You can map value by header by convert excel file to json first and then you can map to object with object mapper