x1nfly
5/15/2019 - 1:16 AM

解析Excel

<?php
// composer require phpoffice/phpspreadsheet

use PhpOffice\PhpSpreadsheet\IOFactory;

$file = $request->file('excel')->move('../public/upload/excel/logistics');
try {
    $recordRepetition = [];
    $spreadsheet = IOFactory::load('./upload/excel/logistics/' . $file->getSaveName());
    $worksheet = $spreadsheet->getActiveSheet();
    foreach ($worksheet->getRowIterator(2) as $row) { // getRowIterator() 方法可以选择从第几行开始
        $rowData = [];
        $cellIterator = $row->getCellIterator('A', 'B'); // getCellIterator() 方法可以选择列区间
        $cellIterator->setIterateOnlyExistingCells(true); // 过滤未设置内容的单元格
        foreach ($cellIterator as $cell) {
            $rowData[] = $cell->getValue();
        }
    }
} catch (\PhpOffice\PhpSpreadsheet\Reader\Exception $e) {
    return $this->httpError($e->getMessage());
} catch (\PhpOffice\PhpSpreadsheet\Exception $e) {
    return $this->httpError($e->getMessage());
}