Quantcast
Channel: Active questions tagged excel - Stack Overflow
Viewing all articles
Browse latest Browse all 88854

Looping through columns and rows in PHPExcel to create objects

$
0
0

I'm using PHPExcel library to loop through my excel spreadsheet. This is my current script :

try
{
    $inputfiletype = PHPExcel_IOFactory::identify($inputfilename);
    $objReader = PHPExcel_IOFactory::createReader($inputfiletype);
    $objPHPExcel = $objReader->load($inputfilename);
      $r = array( 'd' => true );
}
catch(Exception $e)
{
    die('Error loading file "'.pathinfo($inputfilename,PATHINFO_BASENAME).'": '.$e->getMessage());
      $r = array( 'd' => false );
}

//  Get worksheet dimensions
$sheet = $objPHPExcel->getSheet(0); 
$highestRow = $sheet->getHighestRow(); 
$highestColumn = $sheet->getHighestColumn();

//  Loop through each row of the worksheet in turn
for ($row = 1; $row <= $highestRow; $row++)
{ 
    //  Read a row of data into an array
    $rowData = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row, NULL, TRUE, FALSE);
    echo $rowData[0][0] . '</br>';

}

This is my spreadsheet :

enter image description here

What I'm trying to do is loop through each row, ignore the nulls and produce an object with the column name. For the example in the above image row 5 would produce :

{HVHV001OXLG:{
           A5AR:"152",
           A5AM:"988",
           A5AL:"100"
}
}

Is it possible to produce something like this from the format of my spreadsheet?


Viewing all articles
Browse latest Browse all 88854

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>