/ ** * read the Excel conversion array * * @param string $excelFile file path * @param int $startRow The number of lines to start reading * @param int $en Drow end of rows read * @return array*/PrivatefunctionReadfromexcel ($excelFile,$startRow= 1,$endRow= 100) { include_once'./core/common/phpexcel.php '; include_once'./core/common/phpexcelreadfilter.php '; $excelType= Phpexcel_iofactory::identify ($excelFile); $excelReader= \phpexcel_iofactory::createreader ($excelType); if(Strtoupper($excelType) = = ' CSV ') { $excelReader->setinputencoding (' GBK '); } if($startRow&&$endRow) { $excelFilter=NewPhpexcelreadfilter (); $excelFilter->startrow =$startRow; $excelFilter->endrow =$endRow; $excelReader->setreadfilter ($excelFilter); } $phpexcel=$excelReader->load ($excelFile); $activeSheet=$phpexcel-Getactivesheet (); $highestColumn=$activeSheet->gethighestcolumn ();//the letter that corresponds to the last column, for example, line 1th is A. $highestColumnIndex= \phpexcel_cell::columnindexfromstring ($highestColumn);//total number of columns $data=Array(); for($row=$startRow;$row<=$endRow;$row++) { for($col= 0;$col<$highestColumnIndex;$col++) { $data[$row][] = (string)$activeSheet->getcellbycolumnandrow ($col,$row),GetValue (); } if(implode($data[$row], ') = = ') { unset($data[$row]); } } return $data; }
$rowSize= 200; $startRow= 2;//start reading from the second line$endRow=$rowSize;$excel _orders=Array(); while(true) {$excel _orders=$this->readfromexcel (dirname(dirname(home_path)). ' /upload/'.$newname,$startRow,$endRow); if(Empty($excel _orders)) { break; } $startRow=$endRow+ 1; $endRow=$endRow+$rowSize;}
/** * Read the Excel filter class separate file*/classPhpexcelreadfilterImplementsPhpexcel_reader_ireadfilter { public $startRow= 1; public $endRow; public functionReadcell ($column,$row,$worksheetName= ' ') { if(!$this-Endrow) { return true; } if($row>=$this->startrow &&$row<=$this-Endrow) { return true; } return false; }}
Description: the user imported the order data, but after the user imported about 8k of data, PHP began to explode red.
Read some of the information, it is because the Phpexcel class read, if you use
$PHPExcel $PHPReader->load ($newfilename);
Load way to read the file, is to read all the contents of the file and stored in memory, and then read the content, is directly read from memory, extremely consumption of resources.
The result of the above code fragment implementation is that I need that piece (n-m line) of content, I only read that part of the content, will not load the entire file.
finally, the use of the variables can be done $a = null; /unset ($a); To release Resources.
I wish you a smooth job!
Phpexcel large file block level reads faster and consumes less resources