All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.afu.utils.excel.ExcelReader Maven / Gradle / Ivy

There is a newer version: 0.0.55-RELEASE
Show newest version
package io.afu.utils.excel;

import io.afu.common.exception.BaseException;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
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.io.InputStream;
import java.util.*;

public class ExcelReader {

    public static Map> readExcel(String filePath){
        Map> result = new HashMap>();
        try {
            FileInputStream inputStream = new FileInputStream(new File(filePath));
            Workbook workbook = new XSSFWorkbook(inputStream);
            Sheet firstSheet = workbook.getSheetAt(0);
            Iterator iterator = firstSheet.iterator();
            while (iterator.hasNext()) {
                Row nextRow = iterator.next();
                Iterator cellIterator = nextRow.cellIterator();
                List row = new ArrayList();
                while (cellIterator.hasNext()) {
                    Cell cell = cellIterator.next();
                    row.add(cell.getStringCellValue());
                }
                result.put(row.get(0),row);
            }
            workbook.close();
            inputStream.close();

        }catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 读取第一列数据
     * @param filePath 文件路径
     * @return 第一列数据
     */
    public static List readFirstCell(String filePath) {
        List firstCells = new ArrayList();
        try {
            FileInputStream inputStream = new FileInputStream(new File(filePath));
            Workbook workbook = new XSSFWorkbook(inputStream);
            Sheet firstSheet = workbook.getSheetAt(0);
            Iterator iterator = firstSheet.iterator();
            while (iterator.hasNext()) {
                Row nextRow = iterator.next();
                Iterator cellIterator = nextRow.cellIterator();
                List row = new ArrayList();
                while (cellIterator.hasNext()) {
                    Cell cell = cellIterator.next();
                    row.add(cell.getStringCellValue());
                }
                firstCells.add(row.get(0));
            }
            workbook.close();
            inputStream.close();

        }catch (Exception e){
            e.printStackTrace();
        }
        return firstCells;
    }

    /**
     * 按照列序号来读取列
     * @param filePath Excel文件路劲
     * @param index 列序号
     * @return 该列数据
     */
    public static List readCellAt(String filePath,Integer index) {
        List firstCells = new ArrayList();
        try {
            FileInputStream inputStream = new FileInputStream(new File(filePath));
            Workbook workbook = new XSSFWorkbook(inputStream);
            Sheet firstSheet = workbook.getSheetAt(0);
            Iterator iterator = firstSheet.iterator();
            while (iterator.hasNext()) {
                Row nextRow = iterator.next();
                Iterator cellIterator = nextRow.cellIterator();
                List row = new ArrayList();
                while (cellIterator.hasNext()) {
                    Cell cell = cellIterator.next();
                    row.add(cell.getStringCellValue());
                }
                firstCells.add(row.get(index));
            }
            workbook.close();
            inputStream.close();

        }catch (Exception e){
            e.printStackTrace();
        }
        return firstCells;
    }






}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy