解析Excel工具类

pom依赖:

        <!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.15</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.15</version>
        </dependency>

工具类:

package com.swj.service;

import com.swj.basic.exception.ServiceException;
import com.swj.basic.helper.StringHelper;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * 解析Excel工具类
 *
 * @author chenlg
 * @since 2018/10/11
 **/
public class ExcelHelper {

    /**
     * 获取某一行的数据
     * @author chenlg
     * @since 2018/10/11
     * @param excel 文件
     * @param rowNum 列号
     * @return java.util.List<java.lang.String>
     */
    public static List<String> getRow(File excel, Integer rowNum) {
        if (excel == null || rowNum < 0) {
            throw new ServiceException("Invalid arguments at ExcelHelper.getRow");
        }
        try {
            Workbook wb = validateFile(excel);
            //开始解析
            List<String> result = null;
            Sheet sheet = wb.getSheetAt(0);
            //开始解析
                Row row = sheet.getRow(rowNum);
                if (row != null) {
                    int firstCellIndex = row.getFirstCellNum();
                    int lastCellIndex = row.getLastCellNum();
                    for (int cIndex = firstCellIndex; cIndex < lastCellIndex; cIndex++) {
                        Cell cell = row.getCell(cIndex);
                        result.add(cell == null ? "" : cell.toString());
                    }
                }
            return result;
        } catch (Exception e) {
            throw new ServiceException("解析excel文件失败");
        }
    }

    /**
     * 根据标题获取某一列的数据
     * @author chenlg
     * @since 2018/10/11
     * @param excel 文件
     * @param title 列标题
     * @return java.util.List<java.lang.String>
     */
    public static List<String> getColumnByTitle(File excel, String title) {
        Integer colNum = getColumnNum(excel, title);
        if (colNum!=null && colNum>=0){
            return getColumn(excel,colNum);
        }else{
            return new ArrayList<>();
        }
    }

    /**
     * 获取某一列的数据
     * @author chenlg
     * @since 2018/10/11
     * @param excel 文件
     * @param colNum 列号
     * @return java.util.List<java.lang.String>
     */
    public static List<String> getColumn(File excel, Integer colNum) {
        if (excel == null || colNum < 0) {
            throw new ServiceException("Invalid arguments at ExcelHelper.getColumn");
        }
        try {
            Workbook wb = validateFile(excel);
            //开始解析
            List<String> result = new ArrayList<>();
            Sheet sheet = wb.getSheetAt(0);
            int firstRowIndex = sheet.getFirstRowNum();
            int lastRowIndex = sheet.getLastRowNum();
            for (int rIndex = firstRowIndex; rIndex <= lastRowIndex; rIndex++) {
                Row row = sheet.getRow(rIndex);
                if (row != null) {
                    Cell cell = row.getCell(colNum);
                    result.add(cell == null ? "" : cell.toString());
                }
            }
            return result;
        } catch (Exception e) {
            throw new ServiceException("解析excel文件失败");
        }
    }

    /**
     * 根据列名获取列号
     *
     * @author chenlg
     * @since 2018/10/11
     * @param colName 列名
     * @param excel   文件
     * @return java.lang.Integer
     */
    public static Integer getColumnNum(File excel, String colName) {
        if (excel == null || StringHelper.isNullOrEmpty(colName)) {
            throw new ServiceException("Invalid arguments at ExcelHelper.getColumnNum");
        }
        Workbook wb = validateFile(excel);
        //开始解析
        Sheet sheet = wb.getSheetAt(0);
        Row title = sheet.getRow(0);
        if (title == null) {
            throw new ServiceException("excel文件标题行为空");
        }
        int firstCellIndex = title.getFirstCellNum();
        int lastCellIndex = title.getLastCellNum();
        for (int cIndex = firstCellIndex; cIndex < lastCellIndex; cIndex++) {
            Cell cell = title.getCell(cIndex);
            if (cell != null && cell.toString().equals(colName)) {
                return cell.getColumnIndex();
            }
        }
        return -1;
    }

    private static Workbook validateFile(File excel) {
        try {
            //判断文件是否存在
            String[] split = excel.getName().split("\\.");
            Workbook wb;
            //根据文件后缀(xls/xlsx)进行判断
            if ("xls".equals(split[1])) {
                FileInputStream fis = new FileInputStream(excel);
                wb = new HSSFWorkbook(fis);
            } else if ("xlsx".equals(split[1])) {
                wb = new XSSFWorkbook(excel);
            } else {
                throw new ServiceException("文件类型错误!");
            }
            return wb;
        } catch (Exception e) {
            throw new ServiceException("解析excel文件失败");
        }
    }

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值