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.
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.
来源:https://stackoverflow.com/questions/29456375/phpexcel-toarray-skip-first-header-row