使用Java中的Apache POI从Excel文件中删除空行

时间:2015-05-10 12:45:28

标签: java excel apache-poi

如何使用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

0 个答案:

没有答案