java 如何检查excel文件是否为空白?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35718868/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to check if excel file is blank?
提问by Harleen
I have excel file (*.xls
or *.xlxs
) which can have records or no records. First I store the file in some temp location, then copy the contents of the file and then tried to read the file. This scenario works fine if the excel sheet contains records but if the sheet has no record and the file is empty. This scenario does not work. I am using apache-poi to read the excel file contents.
我有可以有记录或没有记录的excel 文件(*.xls
或*.xlxs
)。首先,我将文件存储在某个临时位置,然后复制文件的内容,然后尝试读取该文件。如果 Excel 工作表包含记录但工作表没有记录且文件为空,则此方案工作正常。这种情况是行不通的。我正在使用 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;
}
I get IOException : unable to read entire header 0 bytes read expected 512 bytes
. Can somebody suggest me a way to get rid of this exception and check the excel file is empty?
我明白了IOException : unable to read entire header 0 bytes read expected 512 bytes
。有人可以建议我摆脱这个异常并检查excel文件是否为空吗?
回答by Rudzianko?
Googling and some experiments with org.apache.poi
library suggest to me that it is abnormal to check if xls
file is empty without knowing even approximately the data structure within it. It's as hard as to say if, for example, Oracle BD
is empty.
Any way, you are able to fetch quantity of sheets then iterate over all of them, check rows and cells and consider file is empty if you found nothing.
谷歌搜索和一些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;
}
回答by Pessi
Using Apache Poi (org.apache.poi) for the solution and skipping the parts where I actually get the Workbook (excel) and get access to it's sheets.
使用 Apache Poi (org.apache.poi) 作为解决方案并跳过我实际获取工作簿 (excel) 并访问其工作表的部分。
If you want to know if sheet is empty use this, modify the solution to loop through sheets to find out if all of the sheets in the file are empty.
如果您想知道工作表是否为空,请使用此方法,修改解决方案以循环访问工作表以找出文件中的所有工作表是否为空。
if (sheet.getLastRowNum() == 0 && sheet.getRow(0) == null) {
// This is the case when the sheet is empty!
}
getLastRowNum() - returns 0 in case of empty sheet or if there is only data in the first row
getLastRowNum() - 如果工作表为空或第一行只有数据,则返回 0
getRow(0) - returns null if there is no data in the row
getRow(0) - 如果行中没有数据,则返回 null
NOTE: the solution doesn't take empty strings, spaces, empty lines and etc. into account
注意:该解决方案不考虑空字符串、空格、空行等
回答by ThanhPT
if (cell == null || cell.getCellType() == Cell.CELL_TYPE_BLANK) {
return true;
}
return false;
Try this one !
试试这个!