激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服務(wù)器之家 - 編程語言 - JAVA教程 - java實(shí)現(xiàn)excel導(dǎo)入數(shù)據(jù)的工具類

java實(shí)現(xiàn)excel導(dǎo)入數(shù)據(jù)的工具類

2019-11-14 15:27java教程網(wǎng) JAVA教程

這篇文章主要介紹了java實(shí)現(xiàn)的excel導(dǎo)入數(shù)據(jù)的工具類,需要的朋友可以參考下

導(dǎo)入Excel數(shù)據(jù)的工具類,調(diào)用也就幾行代碼,很簡單的。

 

復(fù)制代碼代碼如下:


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導(dǎo)入的工具類.
 */
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);
    /**
     * 私有化構(gòu)造器
     */
    private ExcelUtils(){

    }

    /**
     * 獲取excel文件中的數(shù)據(jù)對(duì)象
     *
     * @param is                        excel
     * @param excelColumnNames          excel中每個(gè)字段的英文名(應(yīng)該與pojo對(duì)象的字段名一致,順序與excel一致)
     * @return                          excel每行是list一條記錄,map是對(duì)應(yīng)的"字段名-->值"
     * @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();
        }

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

        //雙重for循環(huán)取出數(shù)據(jù)
        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;
    }

    /**
     * 獲取導(dǎo)入數(shù)據(jù)為對(duì)象的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中每一行的數(shù)據(jù)是否合法

 

復(fù)制代碼代碼如下:

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

 

調(diào)用部分

 

復(fù)制代碼代碼如下:

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;
            }
        });

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 一区二区三区在线观看av | 中文字幕亚洲一区二区三区 | 久久久久久久久成人 | 舌头伸进添的我好爽高潮网站 | 国产91精品亚洲精品日韩已满 | 亚欧美一区二区 | 成年免费网站 | 黄视频免费在线 | 250pp久久新 黄色网址免费在线播放 | 国产一级免费在线视频 | 国产成人自拍小视频 | 黄色大片高清 | 我爱我色成人网 | 国产精品.com| 毛片大全免费看 | 欧美性色大片 | 国产无限资源在线观看 | 国产一区免费 | 国产精品亚洲综合一区二区三区 | 久久老司机精品视频 | 亚洲精品午夜电影 | 国产乱xxxx| 国产美女视频一区二区三区 | 久久精品视频网址 | 竹内纱里奈和大战黑人 | 92看片淫黄大片一级 | 欧美精品一区二区三区久久久 | 久久91亚洲精品久久91综合 | 国产精品视频海角社区88 | 久久精品片| 高颜值美女啪啪 | 免看黄大片aa| 国产精品亚洲欧美 | 美女露100%无遮挡 | 国产色视频在线观看免费 | 羞羞的视频在线免费观看 | 被玩坏了的女老师(高h np) | 精品一区二区三区免费看 | 一区二区三区国产好的精 | 欧洲伊人网 | 欧美在线成人影院 |