php读取Excel文件内容(格式化为关联数组)

1、composer引入php处理excel包:box/spout

 

2、处理函数示例:

    /**
     * @param string $filePath 文件绝对路径
     * @return array
     * @throws
     */
    public function readFromExcel(string $filePath)
    {
        $reader = ReaderEntityFactory::createXLSXReader();
        $reader->open($filePath);

        $fileDataArr = [];
        foreach ($reader->getSheetIterator() as $sheet) {
            foreach ($sheet->getRowIterator() as $RowKey => $row) {
                if ($RowKey == 1) {
                    continue;
                }

                foreach($row->getCells() as $CellKey => $value){
                    switch ($CellKey){
                        case 1:
                            $orderId = $value->getValue();
                            break;

                        default:
                        case 0:
                            $shopId = $value->getValue();
                            break;
                    }
                }
                $fileDataArr[$shopId][] = $orderId;
            }
            break;
        }
        $reader->close();
        return fileDataArr;
    }

  

posted @ 2022-10-08 12:08  皮皮留声机  阅读(109)  评论(0编辑  收藏  举报