簡體   English   中英

使用 java 從 xlsx 中刪除特定行

[英]Delete a specific row from the xlsx using java

我正在嘗試將文件名與 xlsx 表進行比較……如果文件名與 Excel 表的值匹配……我想從 Excel 表中刪除該特定行……下面是我所擁有的代碼到目前為止嘗試過...

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import org.apache.poi.EncryptedDocumentException;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.sl.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class try2 {
    public static void main(String[] args)
            throws FileNotFoundException, IOException {
        File[] files=  new File
                ("C:\\wamp\\www\\ptry\\sample\\xl").listFiles();
        String s = null;
        for(File file:files){
            s=file.getName();
            s=s.replaceAll(".xlsx", "");
            }
            File xl=new File("C:\\wamp\\www\\ptry\\sample\\xl.xlsx");
            FileInputStream f=new FileInputStream(xl);
            XSSFWorkbook wb = new XSSFWorkbook (f);
            XSSFSheet sheet = wb.getSheetAt(0);
            int row=sheet.getLastRowNum()+1;
            int colm=sheet.getRow(0).getLastCellNum();
            for(int i=0;i<row;i++){
                XSSFRow r=sheet.getRow(i);
                String m=cellToString(r.getCell(0));
                if(s.equals(m)){
                    System.out.println(m);
                }
            }
        }


    public static String cellToString(XSSFCell cell) {
        int type;
        Object result = null;
        type = cell.getCellType();
        switch (type) {
            case XSSFCell.CELL_TYPE_STRING:
                result = cell.getStringCellValue();
                break;
            case XSSFCell.CELL_TYPE_BLANK:
                result = "";
                break;
            case XSSFCell.CELL_TYPE_FORMULA:
                result = cell.getCellFormula();
        }
        return result.toString();
    }
    }

這里的's'是保存文件名的變量,'m'是保存excel值的變量問題是:如果我使用

   if(s.equals(m))
  {
    System.out.println(m);
  }

如何從EXCEL中刪除匹配的行??? 例如)

文件名:

.xlsx

b.xlsx

.xlsx

excel.xlsx

一種

d

C

我想從 excel.xlsx 中刪除 a 和 b

更新:基於 YASH 建議,我嘗試了以下代碼

                 if(s.equals(m)){
                    System.out.println(m);
                    sheet.removeRow(r);
                }

它從 excel.xlsx(a).... 中刪除了第一個值,並在下面的行中顯示線程“main”中的異常 java.lang.NullPointerException 錯誤

                     String m=cellToString(r.getCell(0));

我試過

              XSSFRow r = sheet.getRow(i);
                    if(r==null){
                        continue;
                    }

它只需要來自 excel.xlsx 的兩個值(a,b)

After removing the row with RemoveRow(r) shift the remaining rows by 1 as shown below to avoid NullPointer Exception

    sheet.RemoveRow(r);   

            int rowIndex = r.RowNum;

            int lastRowNum = sheet.LastRowNum;

            if (rowIndex >= 0 && rowIndex < lastRowNum)
        {
            sheet.ShiftRows(rowIndex + 1, lastRowNum, -1);
        }

終於我得到了答案

        import java.io.File;
        import java.io.FileInputStream;
        import java.io.FileNotFoundException;
        import java.io.FileOutputStream;
        import java.io.IOException;
        import java.util.ArrayList;
        import java.util.Iterator;
        import java.util.List;
        import java.util.logging.Level;
        import java.util.logging.Logger;
        import javax.swing.JButton;
        import org.apache.poi.EncryptedDocumentException;
        import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
        import org.apache.poi.openxml4j.util.ZipSecureFile;
        import org.apache.poi.sl.usermodel.Sheet;
        import org.apache.poi.ss.usermodel.Cell;
        import org.apache.poi.ss.usermodel.Row;
        import org.apache.poi.ss.usermodel.Workbook;
        import org.apache.poi.ss.usermodel.WorkbookFactory;
        import org.apache.poi.xssf.usermodel.XSSFCell;
        import org.apache.poi.xssf.usermodel.XSSFRow;
        import org.apache.poi.xssf.usermodel.XSSFSheet;
        import org.apache.poi.xssf.usermodel.XSSFWorkbook;

        public class try1 {
           public static void main(String[] args)
        throws FileNotFoundException, IOException {
             File[] files=new File("D:\\aa\\a").listFiles();
                   String s = null;
                                   for(File file:files){
                                    s=file.getName();
                                            s=s.replaceAll(".xlsx", "");  

                    File xl=new File("D:\\aa\\w1.xlsx");
                    FileInputStream f=new FileInputStream(xl);

                   XSSFWorkbook wb = new XSSFWorkbook (f);
                    XSSFSheet sheet = wb.getSheetAt(0);
                    int row=sheet.getLastRowNum();
                int colm=sheet.getRow(0).getLastCellNum();
                for(int i=0;i<row;i++){

                    XSSFRow r = sheet.getRow(i);
                    if(r==null){
                       sheet.getRow(i+1);
                        continue;
                    }
                Cell cell=r.getCell(0);
                String m=cellToString(r.getCell(0));
                if(s.equals(m)){
                  System.out.println("s :"+m);
                   sheet.removeRow(r);

                   }}


                FileOutputStream out= 
            new FileOutputStream(new File("D:\\aa\\w1.xlsx"));
               wb.write(out);
                }               
                     }public static String cellToString(XSSFCell cell) {

                int type;
                Object result = null;
                type = cell.getCellType();
               switch (type) {
                 case XSSFCell.CELL_TYPE_STRING:
                    result = cell.getStringCellValue();
                    break;
                case XSSFCell.CELL_TYPE_BLANK:
                    result = "";
                    break;
                case XSSFCell.CELL_TYPE_FORMULA:
                    result = cell.getCellFormula();
                }

                return result.toString();
            }

        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM