Execl util

package com.ying.common;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
 * 
 *
 * @author 
 * @date 2018/1/10
 */
public class ExcelUtils {

    /**
     * 获取Excel内容
     *
     * @param file 文件
     * @param maxRow 最大读取行数
     * @return Excel内容
     * @throws IOException 异常
     */
    public static List<List<JSONObject>> getExcelContent(MultipartFile file, int maxRow) throws IOException {
        List<List<JSONObject>> sheetList = Lists.newArrayList();
        InputStream fis = null;
        try {
            fis = file.getInputStream();
            //创建工作薄
            XSSFWorkbook wb = new XSSFWorkbook(fis);
            int numberOfSheets = wb.getNumberOfSheets();
            for (int i = 0; i < numberOfSheets; i++) {
                XSSFSheet st = wb.getSheetAt(i);
                int lastRow = st.getLastRowNum() <= maxRow ? st.getLastRowNum() : maxRow;

                List<String> sheetHeadList = ExcelUtils.getSheetHeadList(st.getRow(0));
                if (CollectionUtils.isEmpty(sheetHeadList)) {
                    continue;
                }

                List<JSONObject> jsonObjList = Lists.newArrayListWithCapacity(lastRow);
                // 第一行为标题,不取
                for (int rowIndex = 1; rowIndex <= lastRow; rowIndex++) {
                    XSSFRow row = st.getRow(rowIndex);
                    if (null == row) {
                        break;
                    }
                    JSONObject jsonObject = ExcelUtils.getCellContent(row, sheetHeadList);

                    if (null == jsonObject) {
                        break;
                    }
                    jsonObjList.add(jsonObject);
                }
                sheetList.add(jsonObjList);
            }
        } finally {
            IOUtils.closeQuietly(fis);
        }

        return sheetList;
    }

    /**
     * 表格头列表
     *
     * @param row 行
     * @return 表格头列表
     */
    public static List<String> getSheetHeadList(XSSFRow row) {
        if (row == null) {
            return Collections.emptyList();
        }
        short lastCellNum = row.getLastCellNum();
        List<String> headList = Lists.newArrayListWithCapacity(lastCellNum);
        for (int columnIndex = 0; columnIndex <= lastCellNum; columnIndex++) {
            XSSFCell contentCell = row.getCell(columnIndex);
            if (contentCell == null) {
                continue;
            }
            headList.add(getCellValue(contentCell).toString());
        }
        return headList;
    }

    /**
     * 表格内容
     */
    public static JSONObject getCellContent(XSSFRow row, List<String> headList) {
        JSONObject jsonObj = new JSONObject();

        int maxHeadIndex = headList.size() - 1;
        // 当前行最后一侧cell的编号 < 标题头的数目时,认为该行数据不全,跳过
        if (row.getLastCellNum() < maxHeadIndex) {
            return null;
        }
        short lastCellNum = row.getLastCellNum() > maxHeadIndex ? (short) maxHeadIndex : row.getLastCellNum();

        int emptyValueCount = 0;
        for (int columnIndex = 0; columnIndex <= lastCellNum; columnIndex++) {
            XSSFCell contentCell = row.getCell(columnIndex);
            if (contentCell == null) {
                continue;
            }
            String head = headList.get(columnIndex);
            Object cellValue = getCellValue(contentCell);
            if (null == cellValue || StringUtils.isBlank(cellValue.toString())) {
                emptyValueCount++;
            }
            jsonObj.put(head, cellValue);
        }

        // 一行中每个单元格都是空白值,就跳过读取,理解为读取完成
        if (emptyValueCount == headList.size()) {
            return null;
        }
        return jsonObj;
    }

    /**
     * 获取单元格的值
     */
    protected static Object getCellValue(XSSFCell cell) {

        switch (cell.getCellType()) {
            case Cell.CELL_TYPE_NUMERIC:
                return cell.getNumericCellValue();
            case Cell.CELL_TYPE_STRING:
                return cell.getStringCellValue();
            case Cell.CELL_TYPE_FORMULA:
                return cell.getRawValue();
            case Cell.CELL_TYPE_BLANK:
            case Cell.CELL_TYPE_BOOLEAN:
            case Cell.CELL_TYPE_ERROR:
            default:
        }
        return StringUtils.EMPTY;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值