2015-09-20 82 views
-1

我有以下Excel的使用Apache POI和MySQL將記錄插入到數據庫:閱讀多維Excel和導入到DB

Column A Column B Column C Column D Column E V1 V2 V3 100 200 300 Axx Bxx Cxx 1 Description1 1 3 10 2 Description2 2 3 12 3 Description3 3 2 23 4 Description4 1 11 31 5 Description5 23 22 1 我想這個數據要導入到數據庫中三個獨立的表:

第一個表:列C1,C2,C3的值作爲行插入到一個表和後續列(D,E)中,僅考慮前三行。我能夠做到這一點沒有問題,並將列索引作爲id字段存儲在該表中。 表1:

3 V1 100 Axx 4 V2 100 Bxx 5 V3 300 Cxx

第二表:我還能夠插入在該表中的列A和B以及ROWNUM的行的值作爲 表2:

4 1 Description1 5 2 Description2 6 3 Description3 ... ... 第三表:這是我觸底的地方,無法保留細胞圖(C列,第4行)及其價值。預期結果是具有價值的映射表,像這樣: 表3的預期:

4 3 1 4 4 3 4 5 10 試過地圖的ArrayList的,LinkedHashMap的與ArrayList的選擇,但失敗了,在同一個讀迭代中,我不能創建ArrayList的一個ArrayList中,這是表的期望輸出3

請指教一下是使用的HashMap,ArrayList中,或者LinkedHashMap

回答

0

實施方式合適的方法

public int handleSheet(Sheet s){ 
... 
... 
//top level iterator for whole sheet 
Iterator<Row> rowIterator = sheet.iterator(); 
while(rowIterator.hasNext()) { 
Row row = (Row) rowIterator.next(); 
int fcell=row.getPhysicalNumberOfCells(); 
    for (int c = 0; c < fcell; c++) { 
    //invoke second iteration via fn call 

    if (cell.getColumnIndex() > 1) { 
     insertFirstDim(sheet, cell.getColumnIndex()); 
    } 

    if (row.getRowNum > 4){ 
    insertSecondDim(sheet, row.getRowNum()); 
    } 

    insertData(row.getRowNum,cell.getColumnIndex()); 

    } 
} 
.... 
.... 
return status; 
} 

private int insertFirstDim(Sheet s , int columnIndex) { 
     Iterator<Row> rowIterator = sheet.iterator(); 
     columndata = new ArrayList<>(); 
     columndata.add(columnIndex+""); 
     while (rowIterator.hasNext()) { 
      Row row = rowIterator.next(); 
      Iterator<Cell> cellIterator = row.cellIterator(); 
      while (cellIterator.hasNext()) { 
       Cell cell = cellIterator.next(); 
     if(cell.getColumnIndex() == columnIndex){// To match column index 
     //do stuff here insert in DB 
     } 
    } 
} 


private int insertSecondDim(Sheet s , int rowNum) { 
     Iterator<Row> rowIterator = sheet.iterator(); 
     ArrayList<> columndata = new ArrayList<>(); 
     columndata.add(rowNum+""); 
     while (rowIterator.hasNext()) { 
      Row row = rowIterator.next(); 
      Iterator<Cell> cellIterator = row.cellIterator(); 
      while (cellIterator.hasNext()) { 
       Cell cell = cellIterator.next(); 
     //do stuff here 
     } 
    } 
} 

private int insertData(int rowNum,int columnIndex) { 
     Iterator<Row> rowIterator = sheet.iterator(); 
     columndata = new ArrayList<>(); 
     columndata.add(columnIndex+""); 
     while (rowIterator.hasNext()) { 
      Row row = rowIterator.next(); 
      Iterator<Cell> cellIterator = row.cellIterator(); 
      while (cellIterator.hasNext()) { 
       Cell cell = cellIterator.next(); 
     // using rownum and columnIndex stored earlier pick the select cell for values 
     if (rowNum == row.getRowNum() && columnIndex == cellgetColumnIndex()){ 
     // pick cell values 

      } 
     } 
    } 
}