How can I solve the problem of excessive memory usage when phpexcel is used to process large files?

Source: Internet
Author: User
Solve the problem that phpexcel occupies too much memory when processing large files. because phpexcel adopts the memory processing mechanism, when there are too many lines or columns of excel files, it will instantly occupy several hundred MB of memory, I cannot apply for more memory because of the server, so I can only try to solve or avoid the problem fundamentally. for example, after processing an excel file or one sheet, I can release the memory and then process the next one, I searched the internet for details about none of them. I am very much looking forward to it. Solve the problem of excessive memory usage when phpexcel processes large files
Phpexcel uses a memory processing mechanism. when the number of excel files or columns is too large, it will instantly occupy several hundred MB of memory. due to server reasons, I cannot apply for more memory, therefore, we can only solve or avoid the problem fundamentally. for example, after processing an excel file or one sheet, we can release the memory and then process the next one. we searched the internet for a circle, none of them are detailed and complete. please help me tune it up. I am very much looking forward to it ..

PS:
1. my version is 1.76. I have set parameters such as files, which does not work at all;
2. the small file has been called
3. as mentioned above, you don't need to increase the memory limit.




The excel processing code is as follows: an error is returned
Fatal error: Using $ this when not in object context in E: \ wamp \ www \ uploadify \ PHPExcel. php on line 796

Function parse_excel_file ($ filename)
{
$ Php_reader = new PHPExcel_Reader_Excel2007 ();
$ Php_reader = PHPExcel_IOFactory: createReaderForFile ($ filename );
$ Php_excel = $ php_reader-> load ($ filename );
$ Current_sheet = $ php_excel-> getSheet (0 );
$ All_column = $ current_sheet-> getHighestColumn ();
$ All_row = $ current_sheet-> getHighestRow ();
$ List = array ();
For ($ row_index = 2; $ row_index <= $ all_row; $ row_index ++)
{
$ Row_val = '';
For ($ col_index = 'a'; $ col_index <= $ all_column; $ col_index ++)
{
$ Addr = $ col_index. $ row_index;
$ Cell = $ current_sheet-> getCell ($ addr)-> getValue ();
If ($ cell instanceof PHPExcel_RichText)
{
$ Cell = $ cell->__ toString ();

$ Row_val = $ row_val. ','. $ cell;
}
Array_push ($ list, $ row_val );
}
PHPExcel: Destroy ();
Return $ list;
}

The code added to phpexcel. php is as follows:
Public function Destroy (){
Foreach ($ this-> _ workSheetCollection as $ index => $ dummy ){
$ This-> _ workSheetCollection [$ index]-> Destroy ();
$ This-> _ workSheetCollection [$ index] = null;
}
}

Add the following code to worksheet. php:
Public function Destroy (){
Foreach ($ this-> _ cellCollection as $ index => $ dummy ){
$ This-> _ cellCollection [$ index] = null;
}
}

------ Solution ----------------------
How many one-time export records?
------ Solution ----------------------
The call method should be: $ php_excel-> Destroy ();

Phpexcel library memory consumption is too serious when computing all in the memory cache
Once ini_set ('memory _ limit ', '1024m'); only 1 million data records can be exported
Finally, we used csv streaming files, and the world was quite quiet.

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.