如何使用Apache POI删除Excel文档中的空行?以下代码在Excel文件中遇到空行时抛出NullPointerException
。
public class removeemptylines {
public static void main(String args[]) throws IOException,
InvalidFormatException {
FileInputStream file = new FileInputStream(new File("v.xlsx"));
XSSFWorkbook workbook = new XSSFWorkbook(file);
XSSFSheet sheet = workbook.getSheetAt(0);
for (int i = 0; i < sheet.getLastRowNum(); i++) {
XSSFRow row = sheet.getRow(i);
if (row == null) {
sheet.removeRow(row);
} else {
System.out.println("not empty");
}
}
}
}
例如,源文件可能包含行:
viany
gdf
kumar
raj
fff
我希望输出为:
viany
gdf
kumar
raj
fff