2016-04-22 73 views
0

我在這裏有一些問題,我想java讀取我的excel文檔,但如果有一些行我不想讀取,我想跳過這一點。這裏是我的Excel例如:讓我們假設我希望我的java程序讀取一行3 excel exampleJAVA - 從特定行中讀取excel

,並在這裏開始是我的代碼:

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上傳了第4行的值,該怎麼辦?或第5行?或者第一排? 有人可以幫助我做到這一點?前

+1

只需將'i = 0'更改爲'i = 2' –

+0

是的,但我們永遠不知道用戶或客戶端何時上傳其他excel的權利?如果excel上傳了第4行的值,該怎麼辦?或第5行?或者第一排? – Diastowo

回答

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表之前有一個有效的身份證件。

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

我假設我們從來不知道用戶或客戶文檔,他/她可以創建一個從任何行開始的值,我們永遠不會知道。如果excel上傳了第4行的值,該怎麼辦?或第5行?或者第一排? – Diastowo

+0

你見過我的excel圖片嗎?不是我想要跳過的每一行都是空白或爲空 – Diastowo

+1

@Diastowo我更新了我的答案。也許只要該行有一個有效的ID,那麼你可以將它插入到數據庫中。 –