我需要解析一個Excel工作表並從每一行檢索值以將其存儲在數據庫中。目前我正在根據每個單元格所保存的值的類型來執行此操作。這在當前情況下是可以的,因爲我只需要處理2列。但是我有一個新的需求來解析一個擁有12列以上的Excel表格。在這種情況下怎麼做?如果我使用帶表格標題的結構化表格,是否有一種方法可以基於列迭代每一行?如何使用列名迭代在Excel表格中的當前行?
我現在的代碼如下。
File file = new File(UPLOAD_LOCATION + fileUpload.getFile().getOriginalFilename());
FileInputStream excelFile = new FileInputStream(file);
Workbook workbook = new XSSFWorkbook(excelFile);
Sheet datatypeSheet = workbook.getSheetAt(0);
Iterator<Row> iterator = datatypeSheet.iterator();
while (iterator.hasNext()) {
Row currentRow = iterator.next();
Iterator<Cell> cellIterator = currentRow.iterator();
while (cellIterator.hasNext()) {
Cell currentCell = cellIterator.next();
// getCellTypeEnum shown as deprecated for version 3.15
// getCellTypeEnum ill be renamed to getCellType starting
// from version 4.0
if (currentCell.getCellTypeEnum() == CellType.STRING) {
System.out.print(currentCell.getStringCellValue() + "--");
} else if (currentCell.getCellTypeEnum() == CellType.NUMERIC) {
System.out.print(currentCell.getNumericCellValue() + "--");
}
}
我使用下面的外部Apache API進口:
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
有沒有一種方法,我可以做在列標題名稱相同的傳球?
請幫忙。
在此先感謝。
你需要它作爲一個迭代的評論?正常循環會成爲問題嗎? –
@BasilBattikhi沒問題。有沒有辦法這樣做? – eccentricCoder
肯定有一種方法,肯定有一種方法由迭代器,但實際上我不知道如何使用它 –