首页 > 编程语言 >php读取Excel文件内容(格式化为关联数组)

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

时间:2022-10-08 12:23:38浏览次数:78  
标签:格式化 filePath Excel value break location reader php

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;
    }

  

TRANSLATE with x English
Arabic Hebrew Polish
Bulgarian Hindi Portuguese
Catalan Hmong Daw Romanian
Chinese Simplified Hungarian Russian
Chinese Traditional Indonesian Slovak
Czech Italian Slovenian
Danish Japanese Spanish
Dutch Klingon Swedish
English Korean Thai
Estonian Latvian Turkish
Finnish Lithuanian Ukrainian
French Malay Urdu
German Maltese Vietnamese
Greek Norwegian Welsh
Haitian Creole Persian  
  TRANSLATE with COPY THE URL BELOW Back EMBED THE SNIPPET BELOW IN YOUR SITE Enable collaborative features and customize widget: Bing Webmaster Portal Back

标签:格式化,filePath,Excel,value,break,location,reader,php
From: https://www.cnblogs.com/jytblog/p/16768542.html

相关文章