Pārlūkot izejas kodu

接入了列名顺序校验

4.1.3.A
jueyue pirms 7 gadiem
vecāks
revīzija
49eb2f8691
2 mainītis faili ar 32 papildinājumiem un 5 dzēšanām
  1. +13
    -2
      easypoi-base/src/main/java/cn/afterturn/easypoi/excel/entity/ImportParams.java
  2. +19
    -3
      easypoi-base/src/main/java/cn/afterturn/easypoi/excel/imports/ExcelImportServer.java

+ 13
- 2
easypoi-base/src/main/java/cn/afterturn/easypoi/excel/entity/ImportParams.java Parādīt failu

@@ -1,6 +1,6 @@
/**
* Copyright 2013-2015 JueYue (qrb.jueyue@gmail.com)
*
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
@@ -19,7 +19,7 @@ import cn.afterturn.easypoi.handler.inter.IExcelVerifyHandler;
/**
* 导入参数设置
*
*
* @author JueYue
* 2013-9-24
* @version 1.0
@@ -86,6 +86,10 @@ public class ImportParams extends ExcelBaseParams {
* 导入时校验数据模板,是不是正确的Excel
*/
private String[] importFields;
/**
* 导入时校验excel的标题列顺序。依赖于importFields的配置顺序
*/
private boolean needCheckOrder = false;
/**
* Key-Value 读取标记,以这个为Key,后面一个Cell 为Value,多个改为ArrayList
*/
@@ -225,4 +229,11 @@ public class ImportParams extends ExcelBaseParams {
this.verfiyGroup = verfiyGroup;
}
public boolean isNeedCheckOrder() {
return needCheckOrder;
}
public void setNeedCheckOrder(boolean needCheckOrder) {
this.needCheckOrder = needCheckOrder;
}
}

+ 19
- 3
easypoi-base/src/main/java/cn/afterturn/easypoi/excel/imports/ExcelImportServer.java Parādīt failu

@@ -43,6 +43,7 @@ import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
@@ -295,7 +296,7 @@ public class ExcelImportServer extends ImportBaseService {
*/
private Map<Integer, String> getTitleMap(Iterator<Row> rows, ImportParams params,
List<ExcelCollectionParams> excelCollection) {
Map<Integer, String> titlemap = new HashMap<Integer, String>();
Map<Integer, String> titlemap = new LinkedHashMap<Integer, String>();
Iterator<Cell> cellTitle;
String collectionName = null;
ExcelCollectionParams collectionParams = null;
@@ -469,10 +470,25 @@ public class ExcelImportServer extends ImportBaseService {
List<ExcelCollectionParams> excelCollection) {
if (params.getImportFields() != null) {
for (int i = 0, le = params.getImportFields().length; i < le; i++) {
if (!titlemap.containsValue(params.getImportFields()[i])) {
if (params.isNeedCheckOrder()) { // 同时校验列顺序
if (params.getImportFields().length != titlemap.size()) {
LOGGER.error("excel列顺序不一致");
throw new ExcelImportException(ExcelImportEnum.IS_NOT_A_VALID_TEMPLATE);
}
int i = 0;
for (String title : titlemap.values()) {
if (!StringUtils.equals(title, params.getImportFields()[i++])) {
LOGGER.error("excel列顺序不一致");
throw new ExcelImportException(ExcelImportEnum.IS_NOT_A_VALID_TEMPLATE);
}
}
} else {
for (int i = 0, le = params.getImportFields().length; i < le; i++) {
if (!titlemap.containsValue(params.getImportFields()[i])) {
throw new ExcelImportException(ExcelImportEnum.IS_NOT_A_VALID_TEMPLATE);
}
}
}
} else {
Collection<ExcelImportEntity> collection = excelParams.values();


Notiek ielāde…
Atcelt
Saglabāt