我想读取.xlsx文件,以获取具有特定列名的每一行中的值,并将它们保存在字符串数组中。
Sample data from file.xlsx
A B C
A123 B123 C123
A456 B456 C456
A789 B789 C789
A999
This is the code that try to get values from column A, but it's not get the correct result.
InputStream is = file.getInputstream();
int rowTotal = 0;
XSSFWorkbook wb = new XSSFWorkbook(is);
XSSFSheet sheet = wb.getSheetAt(0);
XSSFRow row = sheet.getRow(0);
rowTotal = sheet.getLastRowNum();
if((rowTotal > 0) || (sheet.getPhysicalNumberOfRows() > 0)) {
rowTotal++;
log.debug("rowTotal : "+rowTotal);
int colNum = -1;
for(int i = 0; i <= rowTotal; i++) {
if(row.getCell(i).getStringCellValue().trim().equals("A"))
colNum = i;
row = sheet.getRow(i+1);
XSSFCell cellA = row.getCell(colNum);
DataFormatter formatter = new DataFormatter();
String valueA = formatter.formatCellValue(cellA);
log.debug("Data from col A : "+valueA);
}
}
}
The result from log are..
rowTotal : 5
Data from col A : A123
Data from col A : A456
Data from col A : A789
Why it's not print "A999" ?