繁体   English   中英

读取多维Excel并导入DB

[英]Reading multi-dimensional Excel and importing to DB

我有以下 Excel 可以使用 Apache POI 和 MySQL 将记录插入到数据库中:

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 to 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尝试了Map of ArrayLists、LinkedHashMap 和Arraylist 的选项,但失败了,在同一次读取迭代中,我无法创建Arraylist 的ArrayList,这是表3 的所需输出.

请告知使用 HashMap、ArrayList 或 LinkedHashMap 的合适方法

实施方式

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

          }
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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