All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.springbootPlus.excel.result.ExcelImportResult Maven / Gradle / Ivy

package com.github.springbootPlus.excel.result;

import com.github.springbootPlus.excel.parsing.ExcelError;
import org.apache.commons.collections.CollectionUtils;

import java.util.*;

/**
 * Excel导入结果
 *
 * @author lisuo
 */
public class ExcelImportResult {

    int titleIndex = 0;


    /**
     * 头信息,标题行之前的数据,每行表示一个List,每个Object存放一个cell单元的值
     */
    private List> header = null;

    private List title = null;

    /**
     * JavaBean集合,从标题行下面解析的数据
     */
    private List listBean;

    /**
     * Errors
     */
    private List errors = new ArrayList();

    public List> getHeader() {
        return header;
    }

    public void setHeader(List> header) {
        this.header = header;
    }

    public List getTitle() {
        return title;
    }

    public void setTitle(List title) {
        this.title = title;
    }

    @SuppressWarnings("unchecked")
    public  List getListBean() {
        return (List) listBean;
    }

    public void setListBean(List listBean) {
        this.listBean = listBean;
    }

    public List getErrors() {
        return errors;
    }

    public int getTitleIndex() {
        return titleIndex;
    }

    public void setTitleIndex(int titleIndex) {
        this.titleIndex = titleIndex;
    }

    public List getSortedErrors() {
        HashSet hashSet = new HashSet(errors);
        ExcelError[] errorsResult = hashSet.toArray(new ExcelError[]{});
        Arrays.sort(errorsResult, new Comparator() {
            @Override
            public int compare(ExcelError o1, ExcelError o2) {
                return o1.getRow() - o2.getRow();
            }
        });
        return Arrays.asList(errorsResult);
    }

    @SuppressWarnings("unchecked")
    public  List getValidListBean() {
        if (listBean == null) return null;
        List result = new ArrayList<>(listBean.size());
        for (int i = 0; i < listBean.size(); i++) {
            if (!isErrorRow(i)) {
                result.add((T) listBean.get(i));
            }
        }
        return (List) result;
    }

    private boolean isErrorRow(int i) {
        for (ExcelError error : errors) {
            int idx = error.getRow() - (titleIndex + 1) - 1;
            if (i == idx) {
                return true;
            }
        }
        return false;
    }

    /**
     * 导入是否含有错误
     *
     * @return true:有错误,false:没有错误
     */
    public boolean hasErrors() {
        return CollectionUtils.isNotEmpty(errors);
    }

    /**
     * 导入是否有数据
     *
     * @return true:有 ,false:没有
     */
    public boolean hasData() {
        return CollectionUtils.isNotEmpty(listBean);
    }

}