Home >Backend Development >PHP Tutorial >Solution to error when exporting Excel file with PHPExcel_PHP Tutorial
phpexcel file is an open source PHP table operation plug-in. Most people who use excel data import and export choose this plug-in, but there will also be problems during use. Let’s look at the problem analysis and research. .
PHPExcel is the most powerful open source project in PHP for importing, exporting, and operating Microsoft Excel. But its system is complex and sometimes difficult to control.
There are many ways to use PHPExcel to export Excel tables on the Internet, so I won’t go into details in this article. However, during use, I discovered an inexplicable problem: that is, using PHPExcel to automatically export excel files can sometimes be exported and opened normally, but sometimes the generated excel files cannot be opened. Excel2007 prompts "It was found that it cannot be read. content".
Use a text editor to open the generated Excel file and find a line prompting "Fatal error: Call to a member function setValue() on a non-object in PHPExcel/Calculation/FormulaParser.php on line 431".
After careful and painful inspection, it was found that among the text values written into the cells, some values started with an equal sign, such as "====China Youth Daily...". As a result, PHPExcel was writing When entering this cell, it is determined to be a formula, so the formula parser is called to calculate the corresponding value, but the calculation cannot be performed correctly (because it is not a formula in the first place), so the generated excel file cannot be opened.
The solution is very simple,
Just filter out the equal sign "=" before writing the text value into the excel cell.
The following are solutions to the two errors:
(1)Fatal error: Maximum execution time of 30 seconds exceeded Error solution
The php.ini file needs to be modified. If it is a server using Ubuntu 9.04 Server, you can find the php.ini file under /etc/php5/apache2/. If it is Freebsd, you can find php.ini under /usr/local/lib/ document. Edit the following statement with sudo privileges:
Change the following statement:
max_execution_time = 30
Modified to:
max_execution_time = 300
That is, the maximum execution time of a PHP script is extended from 30 seconds to 300 seconds.
Then restart the Apache server. If it is an Ubuntu Server server, you can use the following command:
sudo /etc/init.d/apache2 restart
(2) Solution to Fatal error: Allowed memory size of 16777216 bytes exhausted
You also need to modify the php.ini file:
Change the following statement:
memory_limit = 16M
Modified to:
memory_limit = 512M
That is, the memory that the PHP script can apply for is expanded from 16M to 512M. The specific number can be determined according to your own needs.
You also need to restart the Apache server.
Things to note are:
(1) The above modifications may involve certain risks, such as causing excessive burden on the server. So please double-check whether it is necessary to open larger memory space and longer execution time for PHPExcel or other PHP programs.
(2) After modifying php.ini, the new settings will only take effect after restarting Apache.
(3) Please adjust the settings in php.ini according to your own server's situation.