国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

java實現excel導入數據的工具類

2019-11-26 15:36:48
字體:
來源:轉載
供稿:網友

導入Excel數據的工具類,調用也就幾行代碼,很簡單的。

復制代碼 代碼如下:

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import org.apache.commons.beanutils.BeanUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.InputStream;
import java.util.*;

/**
 * Excel導入的工具類.
 */
public class ExcelUtils {
    private static final Logger logger = LoggerFactory.getLogger(ExcelUtils.class);
    //成功
    public static final Integer STATUS_OK = Integer.valueOf(1);
    //失敗
    public static final Integer STATUS_NO = Integer.valueOf(0);
    /**
     * 私有化構造器
     */
    private ExcelUtils(){

    }

    /**
     * 獲取excel文件中的數據對象
     *
     * @param is                        excel
     * @param excelColumnNames          excel中每個字段的英文名(應該與pojo對象的字段名一致,順序與excel一致)
     * @return                          excel每行是list一條記錄,map是對應的"字段名-->值"
     * @throws Exception
     */
    public static List<Map<String, String>> getImportData(InputStream is, List<String> excelColumnNames) throws Exception {
        logger.debug("InputStream:{}", is);
        if (is == null) {
            return Collections.emptyList();
        }

        Workbook workbook = null;
        try {
            //拿到excel
            workbook = Workbook.getWorkbook(is);
        } catch (BiffException e) {
            logger.error(e.getMessage(), e);
            return Collections.EMPTY_LIST;
        } catch (IOException e) {
            logger.error(e.getMessage(), e);
            return Collections.EMPTY_LIST;
        }
        logger.debug("workbook:{}", workbook);

        if (workbook == null) {
            return Collections.emptyList();
        }

        //第一個sheet
        Sheet sheet = workbook.getSheet(0);
        //行數
        int rowCounts = sheet.getRows() - 1;
        logger.debug("rowCounts:{}", rowCounts);
        List<Map<String, String>> list = new ArrayList<Map<String, String>>(rowCounts - 1);

        //雙重for循環取出數據
        for(int i = 1; i < rowCounts; i++){
            Map<String, String> params = new HashMap<String, String>();
            //i,j i:行 j:列
            for(int j = 0; j < excelColumnNames.size(); j++){
                Cell cell = sheet.getCell(j, i);
                params.put(excelColumnNames.get(j), cell.getContents());
            }

            list.add(params);
        }

        return list;
    }

    /**
     * 獲取導入數據為對象的List
     *
     * @param data
     * @param clazz
     * @param excelColumnNames
     * @param checkExcel
     * @param <T>
     * @return
     * @throws Exception
     */
    public static <T> List<T> makeData(List<Map<String, String>> data, Class<T> clazz, List<String> excelColumnNames, CheckExcel checkExcel) throws Exception {
        if(data == null || data.isEmpty() || clazz == null || checkExcel == null) {
            return Collections.EMPTY_LIST;
        }

        List<T> result = new ArrayList<T>(data.size());
        for(Map<String, String> d : data) {
            if(checkExcel != null && !checkExcel.check(d)) {
                continue;
            }
            T entity = clazz.newInstance();
            for(String column : excelColumnNames) {
                BeanUtils.setProperty(entity, column, d.get(column));
            }

            result.add(entity);
        }

        return result;
    }
}

檢查excel中每一行的數據是否合法

復制代碼 代碼如下:

import java.util.Map;
/**
 * 檢查excel中每一行的數據是否合法
 */
public interface CheckExcel {
    /**
     * 返回true合法
     *
     * @param data      excel中每一行的數據
     * @return
     */
    public boolean check(Map<String, String> data);
}

調用部分

復制代碼 代碼如下:

List<Map<String, String>> data = ExcelUtils.getImportData(is,Constants.EXCEL_COLUMN_NAMES);
List<FeeAllocation> allocations = ExcelUtils.makeData(data, FeeAllocation.class, Constants.EXCEL_COLUMN_NAMES, new CheckExcel() {
            public boolean check(Map<String, String> data) {
                if(StringUtils.isEmpty(data.get("name")))
                    return false;
                return true;
            }
        });

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 乌恰县| 鹤峰县| 商丘市| 海城市| 于都县| 诸城市| 香港 | 大石桥市| 杭锦旗| 香港 | 郎溪县| 邯郸县| 保德县| 枣庄市| 南通市| 绥阳县| 长寿区| 南澳县| 蒙城县| 丰宁| 荆州市| 仪征市| 岳普湖县| 上栗县| 德州市| 棋牌| 嘉祥县| 苗栗市| 卓资县| 奈曼旗| 教育| 元阳县| 禄丰县| 汝城县| 独山县| 大渡口区| 澜沧| 鄂伦春自治旗| 沙田区| 凤城市| 满城县|