【发布时间】:2021-09-06 11:27:58
【问题描述】:
如何使用标题读取 Excel 文件(xlsx)Apache POI,Spring Boot !!!。我知道我们可以使用行读取迭代器和单元迭代器。我想使用标题阅读。
这就是我使用 row iterator 和 cell 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