【问题标题】:Read Excel file using Headers Apache POI Spring Boot使用 Headers Apache POI Spring Boot 读取 Excel 文件
【发布时间】:2021-09-06 11:27:58
【问题描述】:

如何使用标题读取 Excel 文件(xlsxApache POISpring Boot !!!。我知道我们可以使用行读取迭代器和单元迭代器。我想使用标题阅读。

这就是我使用 row iteratorcell iterator 读取 xlsx 文件的方式

    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();
    }

但是如何使用 Header 阅读?

代码

参考图片1:

参考图片2:

【问题讨论】:

  • 只读取第一行,如果那是带有标题的那一行?
  • 例如:使用sheet.getRow(0).getCell(0).getStringCellValue()读取第一列标题。
  • 这在我的情况下不起作用,因为标题值会改变它们的顺序,即标题列是动态的,请参阅图片以供参考

标签: excel spring-boot header apache-poi xlsx


【解决方案1】:

您可以通过先将excel文件转换为json来按标题映射值,然后您可以使用对象映射器映射到对象

【讨论】:

  • 请添加更多详细信息以扩展您的答案,例如工作代码或文档引用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多