PHPExcel toArray skip first header row

寵の児 提交于 2019-12-21 11:01:11

问题


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.


回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/29456375/phpexcel-toarray-skip-first-header-row

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!