Easy poi 4.1.6以上選型建議

優點:

該說說優點了,比如註解式 起手, 導出,可以無侵入代碼,可以侵入代碼寫法, 侵入代碼寫法,可以明確校驗字段,字段轉換 有dataHandle 作者自己定義了一套 對應類型的轉化 formatter 機制

比如時間 --》dataformat
比如數字類型–〉 小樹
比如枚舉類型,自動替換id 爲漢字
支持sax 大批量導入

缺點:

  • 1.這個要用的是忽略空行,這個支持選擇單一主鍵 進行忽略整行 就是數據第一行應該是忽略不了,記個bug
  • 2.不適合位處理機制,拓展性差
  • 3.支持樣式較少

缺點代碼:

 private <T> List<T> importExcel(Collection<T> result, Sheet sheet, Class<?> pojoClass, ImportParams params, Map<String, PictureData> pictures) throws Exception {
        List collection = new ArrayList();
        Map<String, ExcelImportEntity> excelParams = new HashMap();
        List<ExcelCollectionParams> excelCollection = new ArrayList();
        String targetId = null;
        this.i18nHandler = params.getI18nHandler();
        boolean isMap = Map.class.equals(pojoClass);
        if (!isMap) {
            Field[] fileds = PoiPublicUtil.getClassFields(pojoClass);
            ExcelTarget etarget = (ExcelTarget)pojoClass.getAnnotation(ExcelTarget.class);
            if (etarget != null) {
                targetId = etarget.value();
            }

            this.getAllExcelField(targetId, fileds, excelParams, excelCollection, pojoClass, (List)null, (ExcelEntity)null);
        }

        Iterator<Row> rows = sheet.rowIterator();

        for(int j = 0; j < params.getTitleRows(); ++j) {
            rows.next();
        }

        Map<Integer, String> titlemap = this.getTitleMap(rows, params, excelCollection, excelParams);
        this.checkIsValidTemplate(titlemap, excelParams, params, excelCollection);
        Row row = null;
        Object object = null;
        int readRow = 1;

        for(int i = 0; i < params.getStartRows(); ++i) {
            rows.next();
        }

        if (excelCollection.size() > 0 && params.getKeyIndex() == null) {
            params.setKeyIndex(0);
        }

        if (params.isConcurrentTask()) {
            ForkJoinPool forkJoinPool = new ForkJoinPool();
            int endRow = sheet.getLastRowNum() - params.getLastOfInvalidRow();
            if (params.getReadRows() > 0) {
                endRow = params.getReadRows();
            }

            ExcelImportForkJoinWork task = new ExcelImportForkJoinWork(params.getStartRows() + params.getHeadRows() + params.getTitleRows(), endRow, sheet, params, pojoClass, this, targetId, titlemap, excelParams);
            ExcelImportResult forkJoinResult = (ExcelImportResult)forkJoinPool.invoke(task);
            collection = forkJoinResult.getList();
            this.failCollection = forkJoinResult.getFailList();
            return (List)collection;
        } else {
            while(true) {
                do {
                    if (!rows.hasNext() || row != null && sheet.getLastRowNum() - row.getRowNum() <= params.getLastOfInvalidRow() || params.getReadRows() > 0 && readRow > params.getReadRows()) {
                        return (List)collection;
                    }

                    row = (Row)rows.next();
                    if (sheet.getLastRowNum() - row.getRowNum() < params.getLastOfInvalidRow()) {
                        return (List)collection;
                    }
                } while(row.getLastCellNum() < 0);

                if (isMap && object != null) {
                    ((Map)object).put("excelRowNum", row.getRowNum());
                }

                StringBuilder errorMsg = new StringBuilder();
                //有問題: 第一次,除去標題行,object!=null==》永遠false 所以第一數據行爲空 不會忽略
                if (params.getKeyIndex() != null && (row.getCell(params.getKeyIndex()) == null || StringUtils.isEmpty(this.getKeyValue(row.getCell(params.getKeyIndex())))) && object != null) {
                    Iterator var32 = excelCollection.iterator();

                    while(var32.hasNext()) {
                        ExcelCollectionParams param = (ExcelCollectionParams)var32.next();
                        this.addListContinue(object, param, row, titlemap, targetId, pictures, params, errorMsg);
                    }
                } else {
                    object = PoiPublicUtil.createObject(pojoClass, targetId);

                    try {
                        Set<Integer> keys = titlemap.keySet();
                        Iterator var19 = keys.iterator();

                        label136:
                        while(true) {
                            while(true) {
                                Integer cn;
                                Cell cell;
                                String titleString;
                                do {
                                    if (!var19.hasNext()) {
                                        if (object instanceof IExcelDataModel) {
                                            ((IExcelDataModel)object).setRowNum(row.getRowNum());
                                        }

                                        var19 = excelCollection.iterator();

                                        while(var19.hasNext()) {
                                            ExcelCollectionParams param = (ExcelCollectionParams)var19.next();
                                            this.addListContinue(object, param, row, titlemap, targetId, pictures, params, errorMsg);
                                        }

                                        if (this.verifyingDataValidity(object, row, params, isMap, errorMsg)) {
                                            ((List)collection).add(object);
                                        } else {
                                            this.failCollection.add(object);
                                        }
                                        break label136;
                                    }

                                    cn = (Integer)var19.next();
                                    cell = row.getCell(cn);
                                    titleString = (String)titlemap.get(cn);
                                } while(!excelParams.containsKey(titleString) && !isMap);

                                if (excelParams.get(titleString) != null && ((ExcelImportEntity)excelParams.get(titleString)).getType() == BaseEntityTypeConstants.IMAGE_TYPE) {
                                    String picId = row.getRowNum() + "_" + cn;
                                    this.saveImage(object, picId, excelParams, titleString, pictures, params);
                                } else {
                                    try {
                                        this.saveFieldValue(params, object, cell, excelParams, titleString, row);
                                    } catch (ExcelImportException var24) {
                                        if (params.isNeedVerify() && ExcelImportEnum.GET_VALUE_ERROR.equals(var24.getType())) {
                                            errorMsg.append(" ").append(titleString).append(ExcelImportEnum.GET_VALUE_ERROR.getMsg());
                                        }
                                    }
                                }
                            }
                        }
                    } catch (ExcelImportException var25) {
                        LOGGER.error("excel import error , row num:{},obj:{}", readRow, ReflectionToStringBuilder.toString(object));
                        if (!var25.getType().equals(ExcelImportEnum.VERIFY_ERROR)) {
                            throw new ExcelImportException(var25.getType(), var25);
                        }
                    } catch (Exception var26) {
                        LOGGER.error("excel import error , row num:{},obj:{}", readRow, ReflectionToStringBuilder.toString(object));
                        throw new RuntimeException(var26);
                    }
                }

                ++readRow;
            }
        }
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章