我有一个巨大的 Excel 文件,其中包含大量列,如下所示:-
Column1 Column2 Column3 Column4 Column5
abc def ghi
mno pqr
......
这是我编写的用于打印这些值的代码:
try {
FileInputStream inputStr = new FileInputStream(fileName);
XSSFWorkbook xssfWork = new XSSFWorkbook(inputStr) ;
XSSFSheet sheet1 = xssfWork.getSheetAt(0);
Iterator rowItr = sheet1.rowIterator();
while ( rowItr.hasNext() ) {
XSSFRow row = (XSSFRow) rowItr.next();
System.out.println("ROW:-->");
Iterator cellItr = row.cellIterator();
while ( cellItr.hasNext() ) {
XSSFCell cell = (XSSFCell) cellItr.next();
System.out.println("CELL:-->"+cell.toString());
}
}
} catch (Exception e) {
e.printStackTrace();
}
此代码生成的输出是:-
ROW:-->
CELL:-->Column1
CELL:-->Column2
CELL:-->Column3
CELL:-->Column4
CELL:-->Column5
ROW:-->
CELL:-->abc
CELL:-->def
CELL:-->ghi
ROW:-->
CELL:-->mno
CELL:-->pqr
因此,如果我们查看上面的输出,我们可以注意到 POI 库没有拾取我留下空白值的单元格,是否有一种方法可以将这些值设为 null。或者一种识别所显示的值跳过空白单元格的方法?
Thanks.
如果您想获取所有单元格,无论它们是否存在,那么迭代器不适合您。相反,您需要手动获取适当的单元格,可能缺少单元格策略
for(Row row : sheet) {
for(int cn=0; cn<row.getLastCellNum(); cn++) {
// If the cell is missing from the file, generate a blank one
// (Works by specifying a MissingCellPolicy)
Cell cell = row.getCell(cn, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);
// Print the cell for debugging
System.out.println("CELL: " + cn + " --> " + cell.toString());
}
}
有关所有这一切的更多详细信息,请参阅有关迭代单元格的 Apache POI 文档 http://poi.apache.org/spreadsheet/quick-guide.html#Iterator
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)