7

I'm uploading an excel file to website and process it for database use.

I'm using toArray() function to get all rows in a php array.

But I want to skip the first row ( header title row). The rest of rows will be stored in array.

How can I skip the first row.

Note : I can't use rangeToArray() function since there is not fixed range to get rows into array. It is dynamic. All i want is get all rows except first.

3 Answers 3

12

Eko answers half the problem, you can use rangeToArray(); but you don't need to use a loop at all:

$highestRow = $sheet->getHighestRow(); 
$highestColumn = $sheet->getHighestColumn();

$sheetData = $sheet->rangeToArray(
    'A2:' . $highestColumn . $highestRow,
    NULL,TRUE,FALSE
);

Alternatively, use toArray() and then just unset the first element from the returned array

1
  • Yes, thanks @Mark Baker, you get the point I lose it. Much appreciate. :D btw, I forget something, I create that because I need a flexibility of row and Column to get the data. hehe... your answer possibly an answer for that question. (y) Commented Apr 5, 2015 at 11:21
8

You can achieve that using array_shift this:

$toArray = $worksheet->toArray() 
array_shift($toArray);
3

I create a function to read an excel file using PHPExcel like this below :

function Read_Excel($fname=null,$isheet=0,$irow=1,$icol='A'){       
        $inputFileName = $fname;        

        try {
            $inputFileType = PHPExcel_IOFactory::identify($inputFileName);
            $objReader = PHPExcel_IOFactory::createReader($inputFileType);
            $objPHPExcel = $objReader->load($inputFileName);
        } catch(Exception $e) {
            die('Error loading file "'.pathinfo($inputFileName,PATHINFO_BASENAME).'": '.$e->getMessage());
        }

        $sheet = $objPHPExcel->getSheet(intval($isheet)); 
        $highestRow = $sheet->getHighestRow(); 
        $highestColumn = $sheet->getHighestColumn();

        for ($row = intval($irow); $row <= $highestRow; $row++){ 
            //  Read a row of data into an array
            $rowData = $sheet->rangeToArray($icol . $row . ':' . $highestColumn . $row,NULL,TRUE,FALSE);
            $rec_tbl[] = $rowData[0];
        }
        return $rec_tbl;
    }

you just need to change the $irow=1 in the function parameters to get a row you wanted.

4
  • Yes. I thought this first. But, isn't there any 'loopless' way to skip it ? Commented Apr 5, 2015 at 10:44
  • Same though as I created it before, I don't have an option to not doing a 'loopless' way like you said. I've tried it before but no luck finding the way to not use "loopless" that... Commented Apr 5, 2015 at 10:49
  • by the way, if you get a fine solution to not use a "loopless" way, please tell me... :) Commented Apr 5, 2015 at 10:52
  • Sure. I will notify you. Commented Apr 5, 2015 at 11:00

Not the answer you're looking for? Browse other questions tagged or ask your own question.