如何检查excel文件是否为空?

我有Excel文件( *.xls*.xlxs )可以有logging或没有logging。 首先,我将文件存储在某个临时位置,然后复制文件的内容,然后尝试读取文件。 如果Excel工作表包含logging,但是工作表没有logging且文件为空,则此scheme工作正常。 这种情况下不起作用。 我正在使用apache-poi来读取excel文件的内容。

 public static boolean isRowEmpty(Row row) { for (int c = row.getFirstCellNum(); c < row.getLastCellNum(); c++) { Cell cell = row.getCell(c); if (cell != null && cell.getCellType() != Cell.CELL_TYPE_BLANK) { return false; } } return true; } 

我得到IOException : unable to read entire header 0 bytes read expected 512 bytes 。 有人可以build议我摆脱这个exception,并检查Excel文件是空的?

谷歌search和org.apache.poi库的一些实验向我暗示,检查xls文件是否为空是不正常的,甚至不知道其中的数据结构。 例如,如果Oracle BD是空的,就很难说。 任何方式,你都可以获取数量的纸张,然后遍历所有这些,检查行和单元格,并认为文件是空的,如果你什么也没有发现。

 HSSFWorkbook wBook = new HSSFWorkbook(new FileInputStream("your_path")); for(int i = 0; i < wBook.getNumberOfSheets(); i++){ System.out.println("Sheet " + i + " has data: " + isSheetEmpty(wBook.getSheetAt(i))); } boolean isSheetEmpty(HSSFSheet sheet){ Iterator rows = sheet.rowIterator(); while (rows.hasNext()) { HSSFRow row = (HSSFRow) rows.next(); Iterator cells = row.cellIterator(); while (cells.hasNext()) { HSSFCell cell = (HSSFCell) cells.next(); if(!cell.getStringCellValue().isEmpty()){ return true; } } } return false; } 
 if (cell == null || cell.getCellType() == Cell.CELL_TYPE_BLANK) { return true; } return false; 

试试这个!