Excel導入Oracle數據庫

public void expExcelToOracle(String string){
    FileInputStream is;        
    PreparedStatement pstmt=null;
    try {
    private static XSSFRow row;
        Connection conn =BaseDao.getConnnection();       //鏈接數據庫
        is = new FileInputStream(string+".xlsx");        //這裏是導入的xlsx格式的表格
        XSSFWorkbook wb = new XSSFWorkbook(is);
        String sql = "insert into test(id,shuhao,name,tbkucun) values (?,?,?,?)";
        pstmt = conn.prepareStatement(sql);
        for (int i = 0; i < wb.getNumberOfSheets(); i++) {
           XSSFSheet sheet = wb.getSheetAt(i);
           int totalRowNum = sheet.getLastRowNum();
           for (int k = 1; k <= totalRowNum; k++) {
               XSSFRow row = sheet.getRow(k);
               //這裏是三目運算符,若是獲取到excel某一列爲空則賦 ""
               pstmt.setString(1,row.getCell(0)==null?"":row.getCell(0).toString());  
               pstmt.setString(2,row.getCell(1)==null?"":row.getCell(1).toString());    
               pstmt.setString(3,row.getCell(2)==null?"":row.getCell(2).toString());  
               pstmt.setString(4,row.getCell(3)==null?"":row.getCell(3).toString());    
               pstmt.addBatch();
           }  
           if ((i % 12000) == 0){
              pstmt.executeBatch();    //這裏是批處理
              conn.commit();
           }
        }
        pstmt.executeBatch();
        conn.commit();
     } catch (Exception e) {
         e.printStackTrace();
     }
}

這裏傳入一個excel的名字,而後放到項目中就能夠了!java

不過,若是要導入的話必須導入所須要的jar包,這裏我是導入的excel07版的【紅框裏的包是所需jar包】sql

所需jar包能夠到官網上下載。數據庫

相關文章
相關標籤/搜索