Link to home
Start Free TrialLog in
Avatar of Gary Croxford
Gary CroxfordFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Php read spreadsheet into an array

Hello,

I need to extract data from a spreadsheet (xlsx) into an array. I am struggling with phpspreadsheet and really need some help.

My first attempt returns the number of rows in the first sheet of the workbook but when I try to display the contents to the screen in a table the code falls over with http 500 error - I can't see why and don't know how to capture the cause of the error.

      require '../../../../composer/vendor/autoload.php';
      
      use phpoffice\phpspreadsheet\spreadsheet;
      use phpoffice\phpspreadsheet\writer\xlsx;
      
      $xlfile = 'ExcelTest.xlsx';
      $spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load($xlfile);
      
      $xls_data = $spreadsheet->getActiveSheet()->toArray(null, true, true, true);
      
      $nrows = count($xls_data);
      
      echo '<h2> No. Rows: ' . $nrows . '</h2>';   //Code works to here
      
/*      

        // If I include this the whole thing falls over
      echo '<table>';
      foreach ($xls_data[0] as $column){
            echo '<tr><td>' . $column . '</td></tr>';
      }
      echo '</table>';
*/
ExcelTest.xlsx
CESTrack_2.php
ASKER CERTIFIED SOLUTION
Avatar of Terry Woods
Terry Woods
Flag of New Zealand image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Gary Croxford

ASKER

Terry, thanks found the apache error log