从javacode中的excel中删除具有特定值的行

时间:2017-05-04 11:15:02

标签: java excel apache-poi

我想从列F中的值为1的excel中删除所有行,并将其保存在java代码中的新excel中。我的java代码如下。新的excel正在创建,但我无法删除列F中值为1的所有行。我的java代码是:

final String filterValue = "1";
FileInputStream excelFile = new FileInputStream("Users.xls");
            System.out.println("File read successfully!!");

            HSSFWorkbook wb = new HSSFWorkbook(excelFile);
            HSSFSheet sheet = wb.getSheetAt(0);

            HSSFWorkbook wb2 = new HSSFWorkbook();
            wb2 = wb;

            Row row;
            row = sheet.getRow(0);
            int getLastCell=row.getLastCellNum()-1;
            int lastIndex = sheet.getLastRowNum();

            for (int i=0; i<=lastIndex; i++){
                row=sheet.getRow(i);
                if(row.getCell(getLastCell)!=null && (row.getCell(getLastCell).equals(filterValue)))
                          {
                            System.out.println("Removing row "+row);
                           sheet.removeRow(row);
                           //sheet.shiftRows(i, lastIndex, -1);
                        }

                FileOutputStream fileOut = new FileOutputStream("FilteredUserList.xls");
                wb2.write(fileOut);
                fileOut.close();
            }

0 个答案:

没有答案