java excel 데이터 가져오기 도구 클래스 구현

4847 단어 javaexcel
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;
            }
        });

좋은 웹페이지 즐겨찾기