Phpexcel large file block level reads faster and consumes less resources

Source: Internet
Author: User

/ ** * 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.