我试图从外部 Excel 文件 (.xlsx) 中读取一些数据到我的 Java 代码(IDE:Eclipse)中,但我不明白如何获取列。
有人可以帮助我吗?非常感谢!
尝试下一个片段:
public void readXLSX(String excelPath) throws IOException {
try {
FileInputStream fis = new FileInputStream(new File(excelPath));
XSSFWorkbook wb = new XSSFWorkbook(fis);
XSSFSheet sheet = wb.getSheetAt(0);
Iterator<Row> itr = sheet.iterator();
while (itr.hasNext()) {
Row row = itr.next();
Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
switch (cell.getCellType()) {
case Cell.CELL_TYPE_NUMERIC:
System.out.println(cell.getNumericCellValue() + "\t\t");
break;
case Cell.CELL_TYPE_STRING:
System.out.println(cell.getStringCellValue() + "\t\t");
break;
default:
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
我可以通过哪种方式从Excel中获取列? 我正在使用 jdk1.8