JAVA - 从特定行

时间:2016-04-22 02:54:48

标签: java excel apache-poi

我在这里有一些问题,我想java阅读我的excel文档,但是如果有一些我不想阅读的行,我想跳过它。这是我的excel示例:假设我希望我的java程序从第3行开始读取 excel example

这是我的代码:

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");
        }

我假设我们从来不知道用户或客户文档,他/她可以创建一个值从任何行开始的excel,我们永远都不知道。如果他上传的excel值从第4行开始怎么办?还是排5?或者第一排? 有人可以帮助我这样做吗?

之前谢谢

1 个答案:

答案 0 :(得分:0)

这是一种更具活力的方法..

            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);
            }

或者,如果您想以编程方式查找起始行..

在插入testdatapks表之前检查是否有有效的ID。

            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);
                }
            }