How to find out how many rows and columns to read from an Excel file with PHPExcel?

前端 未结 6 623
眼角桃花
眼角桃花 2020-12-12 23:51

With the following code, I am able to read the cells out of an Excel file with PHPExcel.

I currently manually define how many rows and columns to re

6条回答
  •  忘掉有多难
    2020-12-13 00:29

    $spreadsheet = new Spreadsheet();
    $reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader('xls');
    $reader->setReadDataOnly(true);
    $spreadsheet = $reader->load('file location here');
    $worksheet = $spreadsheet->getActiveSheet();
    foreach ($worksheet->getRowIterator() AS $row) {
         $cellIterator = $row->getCellIterator();
         $cellIterator->setIterateOnlyExistingCells(FALSE); // This loops through all cells,
         $cells = [];
               foreach ($cellIterator as $cell) {
                     $cells[] = $cell->getValue();
               }
         $rows[] = $cells;
    }
    
    //blade html
    
                    @foreach($rows[0] as $heading)
                         
                    @endforeach
                
                 @foreach($rows as $key=>$val)
                       @if(!($key == 0))
                            
                                @foreach($val as $value)
                                     
                                @endforeach
                            
                        @endif
                 @endforeach
          
    {{$heading}}
    {{$value}}
    ```

提交回复
热议问题