作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 Apache POI 读取 Excel 文档。如果当前为空,我想从下一行获取单元格值。例如,从第一行获取所有非空单元格值,对于空单元格:移动到下一行并获取值。这是我用来读取 Excel 文档的代码
//every sheet has rows, iterate over them
Iterator<Row> rowIterator = sheet.iterator();
if (rowIterator.hasNext())
rowIterator.next();
while (rowIterator.hasNext())
{
String libelle="";
.....
//Get the row object
Row row = rowIterator.next();
//Every row has columns, get the column iterator and iterate over them
Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext())
{
//Get the Cell object
Cell cell = cellIterator.next();
//check the cell type and process accordingly
//2nd column
switch(cell.getCellType()){
case Cell.CELL_TYPE_STRING:
......
if (row.getCell(5) == null)
{
System.out.println("Cell is Empty in Column:" );
} else if (row.getCell(5).getCellType() == HSSFCell.CELL_TYPE_STRING)
{
libelle= row.getCell(5).getStringCellValue().trim();
}
...
最佳答案
您可以使用Sheet.getRow(int rownumber)
来获取具有特定编号的行。您可以通过 Row.getRowNum()
获取当前行号,通过 Cell.getColumnIndex()
获取单元格索引。
因此,下一行单元格的值将是
Row nextRow = sheet.getRow(row.getRowNum() + 1);
if (nextRow != null)
String value = nextRow.getCell(curCel.getColumnIndex(), Row.CREATE_NULL_AS_BLANK).getStringCellValue();
关于java - 使用 POI 从 Excel 工作表的下一行获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29539428/
我是一名优秀的程序员,十分优秀!