0
votes

I imported an csv file and read that using Maatwebsite Excel in Laravel 4.2 If I know the column name, I can simply get the row value by doing:

Excel::load($file->getRealPath(), function($reader)) { $data = $reader->get(); foreach ($data as $key => $row) { echo $row->I_know_this; });

But what if the csv file contain dynamic column and you don't know what are they?

1

1 Answers

0
votes

try this.

$excel = [];

        Excel::load($destinationPath . $filename, function($reader) use (&$excel) {
            $objExcel = $reader->getExcel();
            $sheet = $objExcel->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);

                $excel[] = $rowData[0];
            }
        });