1. 程式人生 > >利用java獲取excle資料

利用java獲取excle資料

環境:maven工程

 

效果展示:

excle檔案 

 

控制檯輸出:

 

 

1.在pom.xml配置檔案中加入以下程式碼

 

<dependency>

<groupId>org.apache.poi</groupId>

<artifactId>poi</artifactId>

<version>3.14-beta1</version>

</dependency>

<dependency>

<groupId>org.apache.poi</groupId>

<artifactId>poi-ooxml</artifactId>

<version>3.14-beta1</version>

</dependency>

<dependency>

<groupId>org.apache.poi</groupId>

<artifactId>poi-ooxml-schemas</artifactId>

<version>3.14-beta1</version>

</dependency>

 

2.業務程式碼

package com.beeseven.bdes.common.util;

import org.apache.commons.io.IOUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
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 org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Created by xuyaoguo on 2017/8/9.
 */
public class ExcelUtil {

    /**
     * @param cell
     * @return
     */
    public static String getCellStringValue(Cell cell) {
        Object obj = getCellValue(cell);
        String value = null;

        if (null != obj) {
            value = String.valueOf(obj);
        }

        return value;
    }

    /**
     * @param cell
     * @return
     */
    public static Object getCellValue(Cell cell) {
        Object value = null;
        if (null != cell) {
            switch (cell.getCellTypeEnum()) {
                case BLANK:
                    break;
                case ERROR:
                    value = cell.getErrorCellValue();
                    break;
                case STRING:
                    value = cell.getStringCellValue();
                    break;
                case BOOLEAN:
                    value = cell.getBooleanCellValue();
                    break;
                case FORMULA:
                    value = cell.getDateCellValue().getTime();
                    break;
                case NUMERIC:
                    value = cell.getNumericCellValue();
                    DecimalFormat df = new DecimalFormat("0");
                    value = df.format(value);
                    break;
                case _NONE:
            }
        }

        return value;
    }

    /**
     * @param filePath
     * @return
     */
    public static List<List<String>> readExcel(String filePath) {
        String fileSuffix = filePath.substring(filePath.lastIndexOf('.') + 1, filePath.length());

        if (!"xlsx".equalsIgnoreCase(fileSuffix) && !"xls".equalsIgnoreCase(fileSuffix)) {
            throw new RuntimeException("不支援的格式,請上傳xls或xlsx格式的檔案");
        }

        try (FileInputStream fileInputStream = new FileInputStream(filePath)) {
            return readExcel(fileSuffix, fileInputStream);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }

    /**
     * @param fileSuffix
     * @param inputStream
     * @return
     */
    public static List<List<String>> readExcel(String fileSuffix, InputStream inputStream) {

        Workbook workbook = null;
        POIFSFileSystem fileSystem = null;
        List<List<String>> values = null;

        try {

            if ("xlsx".equalsIgnoreCase(fileSuffix)) {
                workbook = new XSSFWorkbook(inputStream);
            } else if ("xls".equalsIgnoreCase(fileSuffix)) {
                fileSystem = new POIFSFileSystem(inputStream);
                workbook = new HSSFWorkbook(fileSystem);
            }

            if (null == workbook) {
                throw new RuntimeException("can not read workbook");
            }

            values = getLists(workbook);

        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            IOUtils.closeQuietly(workbook);
            IOUtils.closeQuietly(fileSystem);
        }

        return values;
    }


    /**
     * @param workbook
     * @return
     */
    public static List<List<String>> getLists(Workbook workbook) {
        Sheet sheet = workbook.getSheetAt(workbook.getActiveSheetIndex());
        int lastRow = sheet.getLastRowNum();
        List<List<String>> values = new ArrayList<>();


        for (int i = 0; i <= lastRow; i++) {
            Row row = sheet.getRow(i);
            if (null == row) {
                continue;
            }
            short cellNum = row.getLastCellNum();
            List<String> rowValue = new ArrayList<>();

            for (int j = 0; j <= cellNum; j++) {
                Cell cell = row.getCell(j);
                if (null != cell) {
                    String value = ExcelUtil.getCellStringValue(cell);
                    rowValue.add(value);
                } else {
                    rowValue.add("");
                }
            }

            values.add(rowValue);
        }


        return values;
    }

    /**
     * 過濾空資料
     *
     * @param values
     */
    public static void filterList(List<List<String>> values) {
        // 過濾空資料
        // 以第一行為準,第一行有多少列
        if (!CollectionUtils.isEmpty(values)) {

            List<String> title = values.get(0);
            int last = title.size() - 1;
            int toDelete = 0;

            // 如果第一行最後一列為空
            for (int i = last; i >= 0; i--) {
                if (StringUtils.isEmpty(title.get(i))) {
                    toDelete++;
                } else {
                    break;
                }
            }

            // 確定列的數量
            int colNum = title.size() - toDelete;
            int len = values.size();

            for (int i = 0; i < len; i++) {

                List<String> row = values.get(i);
                List<String> newRow;

                if (row.size() > colNum) {
                    newRow = new ArrayList(row.subList(0, colNum));
                } else {
                    newRow = new ArrayList<>(row);
                }

                if (newRow.size() < colNum) {
                    int jlen = colNum - newRow.size();
                    for (int j = 0; j < jlen; j++) {
                        newRow.add("");
                    }
                }

                values.set(i, newRow);
            }


            // 過濾掉空行
            Iterator<List<String>> it = values.iterator();
            while (it.hasNext()) {
                List<String> row = it.next();
                boolean allEmpty = row.stream().allMatch(item -> StringUtils.isEmpty(item));
                if (allEmpty) {
                    it.remove();
                }
            }
        }
    }
}