PHPExcel toArray skip first header row

牧云@^-^@ 提交于 2019-12-04 06:14:24

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

You can achieve that using array_shift this:

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

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.

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