简体   繁体   中英

JAVA - Read excel from a specific row

i have some problem here, i want to java read my excel document, but what if there is some of row that i dont want to read, i want to skip that. here is my excel example : lets assume i want my java program read start from row 3 excel example

and here is my code:

public static void main(String[] args) {
    try{
        Class.forName("com.mysql.jdbc.Driver");
        Connection con = (Connection) DriverManager.getConnection(dbconnection+","+dbusername+","+dbPass);
        con.setAutoCommit(false);
        PreparedStatement pstm = null ;
        File file = new File(filepath);
        FileInputStream input = new FileInputStream(file);
        System.out.println(file);
        if (!file.toString().contains("xlsx")){
            System.out.println("hssf");
            POIFSFileSystem fs = new POIFSFileSystem(input);
            HSSFWorkbook wb = new HSSFWorkbook(fs);
            HSSFSheet sheeth = wb.getSheetAt(0);

            Row row;
            for(int i=0; i<=sheeth.getLastRowNum(); i++){
                row = sheeth.getRow(i);

                int id          = (int) row.getCell(0).getNumericCellValue();
                String nama     = row.getCell(1).getStringCellValue();
                String rm       = row.getCell(2).getStringCellValue();
                String nama_ro  = row.getCell(3).getStringCellValue();
                String no_pks   = row.getCell(4).getStringCellValue();
                String ket      = row.getCell(5).getStringCellValue();
                String lob      = row.getCell(6).getStringCellValue();

                String sql = "INSERT INTO testdatapks VALUES('"+id+"','"+nama+"','"+rm+"','"+nama_ro+"','"+no_pks+"','"+ket+"','"+lob+"')";
                pstm = (PreparedStatement) con.prepareStatement(sql);
                pstm.execute();
                System.out.println("Import rows "+i);
            }

            con.commit();
            pstm.close();
            con.close();
            input.close();
            System.out.println("Success import excel to mysql table");
        }

i assume we never know the user or client document, he/she can make a excel which the value start from any row, we never know. what if the excel he uploaded the value start from row 4? or row 5? or maybe first row? can someone help me to do that? thanks before

Here's a more dynamic approach..

            Integer startFrom = 3; // start from 3rd row change as needed
            for(int i=startFrom - 1; i<=sheeth.getLastRowNum(); i++){
                row = sheeth.getRow(i);

                int id          = (int) row.getCell(0).getNumericCellValue();
                String nama     = row.getCell(1).getStringCellValue();
                String rm       = row.getCell(2).getStringCellValue();
                String nama_ro  = row.getCell(3).getStringCellValue();
                String no_pks   = row.getCell(4).getStringCellValue();
                String ket      = row.getCell(5).getStringCellValue();
                String lob      = row.getCell(6).getStringCellValue();

                String sql = "INSERT INTO testdatapks VALUES('"+id+"','"+nama+"','"+rm+"','"+nama_ro+"','"+no_pks+"','"+ket+"','"+lob+"')";
                pstm = (PreparedStatement) con.prepareStatement(sql);
                pstm.execute();
                System.out.println("Import rows "+i);
            }

Or if you want to programmatically find the starting row..

Check if there is a valid Id before inserting to your testdatapks table.

            for(int i=0; i<=sheeth.getLastRowNum(); i++){
                row = sheeth.getRow(i);

                int id  = (int) row.getCell(0).getNumericCellValue();
                if(id >= 0) {
                    String nama     = row.getCell(1).getStringCellValue();
                    String rm       = row.getCell(2).getStringCellValue();
                    String nama_ro  = row.getCell(3).getStringCellValue();
                    String no_pks   = row.getCell(4).getStringCellValue();
                    String ket      = row.getCell(5).getStringCellValue();
                    String lob      = row.getCell(6).getStringCellValue();
                    String sql = "INSERT INTO testdatapks VALUES('"+id+"','"+nama+"','"+rm+"','"+nama_ro+"','"+no_pks+"','"+ket+"','"+lob+"')";
                    pstm = (PreparedStatement) con.prepareStatement(sql);
                    pstm.execute();
                    System.out.println("Import rows "+i);
                }
            }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM