如何从 JAVA 中的 excel 获取列值?
How to get column value from excel in JAVA?
如何在 apache poi 4 中将每行和第 14 列的值读取到字符串变量中。
while (rows.hasNext()) {
Row currentRow = rows.next();
System.out.println(currentRow);
Iterator<Cell> cellsInRow = currentRow.iterator();
while(cellsInRow.hasNext()) {
Cell currentCell = cellsInRow.next();
int cellIndex = currentCell.getColumnIndex();
int columnidx = 14;
String values = currentCell.getStringValue() //how can i get the value for every row for column 14
}
}
如果您查看 文档 ,即 Row
, you will find the very handy getCell(int cellnum)
方法的 javadoc:
Get the cell representing a given column (logical cell) 0-based. If you ask for a cell that is not defined....you get a null
.
这意味着您不应迭代所有单元格:
while (rows.hasNext()) {
Row currentRow = rows.next();
Cell cell14 = currentRow.getCell(13); // 14th cell
String value = (cell14 == null ? null : cell14.getStringCellValue());
// use value here
}
无关,但您不应使用迭代器 while
循环。如果使用 for-each 循环,代码将更简单易读,因此不用:
Iterator<Row> rows = sheet.rowIterator();
while (rows.hasNext()) {
Row currentRow = rows.next();
Iterator<Cell> cellsInRow = currentRow.iterator();
while (cellsInRow.hasNext()) {
Cell currentCell = cellsInRow.next();
}
}
你应该使用:
for (Row currentRow : sheet) {
for (Cell currentCell : currentRow) {
}
}
看看代码改进了多少?
如何在 apache poi 4 中将每行和第 14 列的值读取到字符串变量中。
while (rows.hasNext()) {
Row currentRow = rows.next();
System.out.println(currentRow);
Iterator<Cell> cellsInRow = currentRow.iterator();
while(cellsInRow.hasNext()) {
Cell currentCell = cellsInRow.next();
int cellIndex = currentCell.getColumnIndex();
int columnidx = 14;
String values = currentCell.getStringValue() //how can i get the value for every row for column 14
}
}
如果您查看 文档 ,即 Row
, you will find the very handy getCell(int cellnum)
方法的 javadoc:
Get the cell representing a given column (logical cell) 0-based. If you ask for a cell that is not defined....you get a
null
.
这意味着您不应迭代所有单元格:
while (rows.hasNext()) {
Row currentRow = rows.next();
Cell cell14 = currentRow.getCell(13); // 14th cell
String value = (cell14 == null ? null : cell14.getStringCellValue());
// use value here
}
无关,但您不应使用迭代器 while
循环。如果使用 for-each 循环,代码将更简单易读,因此不用:
Iterator<Row> rows = sheet.rowIterator();
while (rows.hasNext()) {
Row currentRow = rows.next();
Iterator<Cell> cellsInRow = currentRow.iterator();
while (cellsInRow.hasNext()) {
Cell currentCell = cellsInRow.next();
}
}
你应该使用:
for (Row currentRow : sheet) {
for (Cell currentCell : currentRow) {
}
}
看看代码改进了多少?