Home Backend Development PHP Tutorial phpExcel导出大量数据出现内存溢出错误的解决方法_PHP

phpExcel导出大量数据出现内存溢出错误的解决方法_PHP

Jun 01, 2016 pm 12:07 PM
phpexcel memory overflow

phpExcel将读取的单元格信息保存在内存中,我们可以通过

复制代码 代码如下:
PHPExcel_Settings::setCacheStorageMethod()

来设置不同的缓存方式,已达到降低内存消耗的目的!

1、将单元格数据序列化后保存在内存中

复制代码 代码如下:
PHPExcel_CachedObjectStorageFactory::cache_in_memory_serialized;

2、将单元格序列化后再进行Gzip压缩,然后保存在内存中

复制代码 代码如下:
PHPExcel_CachedObjectStorageFactory::cache_in_memory_gzip;

3、缓存在临时的磁盘文件中,速度可能会慢一些

复制代码 代码如下:
PHPExcel_CachedObjectStorageFactory::cache_to_discISAM;

4、保存在php://temp

复制代码 代码如下:
PHPExcel_CachedObjectStorageFactory::cache_to_phpTemp;

5、保存在memcache中

复制代码 代码如下:
PHPExcel_CachedObjectStorageFactory::cache_to_memcache

举例:

第4中方式:

 复制代码 代码如下:
$cacheMethod = PHPExcel_CachedObjectStorageFactory:: cache_to_phpTemp; 
$cacheSettings = array( ' memoryCacheSize '  => '8MB' 
                ); 
PHPExcel_Settings::setCacheStorageMethod($cacheMethod, $cacheSettings);

第5种:

 复制代码 代码如下:
$cacheMethod = PHPExcel_CachedObjectStorageFactory::cache_to_memcache; 
$cacheSettings = array( 'memcacheServer'  => 'localhost', 
                        'memcachePort'    => 11211, 
                        'cacheTime'       => 600 
                      ); 
PHPExcel_Settings::setCacheStorageMethod($cacheMethod, $cacheSettings);

其它的方法

第一个方法,你可以考虑生成多个sheet的方式,不需要生成多个excel文件,根据你数据总量计算每个sheet导出多少行, 下面是PHPExcel生成多个sheet方法:

面是PHPExcel生成多个sheet方法:

复制代码 代码如下:
$sheet = $objPHPExcel->getActiveSheet();
$sheet->setCellValue('A1',$x); 
$sheet->setCellValue('B1',$y);

第二个方法,你可以考虑ajax来分批导出,不用每次刷新页面。

 复制代码 代码如下:
export to Excel
$('#export').click(function() { 
    $.ajax({ 
        url: "export.php",  
        data: getData(),  //这个地方你也可以在php里获取,一般读数据库 
        success: function(response){ 
            window.location.href = response.url; 
        } 
    }) 
});

复制代码 代码如下:
//export.php
$data = $_POST['data'];
$xls = new PHPExcel();
$xls->loadData($formattedData);
$xls->exportToFile('excel.xls');
$response = array(
'success' => true,
'url' => $url
);
header('Content-type: application/json');
echo json_encode($response);
?>

数据量很大的话,建议采用第二种方法,ajax来导出数据,上面方法简单给了个流程,具体你自己补充!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP development: Use PHPExcel to process Excel files PHP development: Use PHPExcel to process Excel files Jun 15, 2023 pm 03:45 PM

With the advent of the digital age, data has become the most important part of our daily lives and work, and Excel files have become one of the important tools for data processing. I believe that many PHP developers will often encounter the use of Excel files for data processing and operations at work. This article will introduce you to the methods and precautions for using the PHPExcel library to process Excel files. What is PHPExcel? PHPExcel is a PHP class

Complete Guide: How to process Excel files using php extension PHPExcel Complete Guide: How to process Excel files using php extension PHPExcel Jul 28, 2023 pm 10:01 PM

Complete Guide: How to Process Excel Files Using PHP Extension PHPExcel Introduction: Excel files are often used as a common format for data storage and exchange when processing large amounts of data and statistical analysis. Using the PHP extension PHPExcel, we can easily read, write and modify Excel files to effectively process Excel data. This article will introduce how to use the PHP extension PHPExcel to process Excel files and provide code examples. 1. Install PHPExc

What is the difference between memory leak and memory overflow? What is the difference between memory leak and memory overflow? Dec 03, 2020 pm 05:22 PM

Difference: Memory overflow means that when the program applies for memory, there is not enough memory space for it to use, and the system can no longer allocate the space you need; memory leak means that the program cannot release the applied memory space after applying for memory. , the harm of a memory leak can be ignored, but too many memory leaks will lead to memory overflow.

What is the difference between memory overflow and memory leak? What is the difference between memory overflow and memory leak? Aug 21, 2023 pm 03:14 PM

The difference between memory overflow and memory leak is that memory overflow means that the program cannot obtain the required memory space when applying for memory, while memory leak means that the memory allocated by the program during running cannot be released normally. Memory overflow is usually due to the needs of the program. The memory exceeds the available memory limit, or recursive calls cause stack space to be exhausted, or memory leaks are caused by unreleased dynamically allocated memory in the program, object references that are not released correctly, or circular references. of.

How to use phpexcel to convert Excel files to CSV files and open them How to use phpexcel to convert Excel files to CSV files and open them Mar 27, 2023 pm 04:16 PM

​PHPEXCEL is an excellent PHP class library for reading and writing Excel files. It provides a very sufficient API that allows us to use PHP to read and write Excel files. Sometimes, we need to convert Excel files into CSV files for use on some occasions. So, this article mainly describes how to use the PHPEXCEL class library to convert Excel files into CSV files and open them.

How to solve the memory overflow problem in PHP development How to solve the memory overflow problem in PHP development Oct 11, 2023 am 09:13 AM

How to solve the memory overflow problem in PHP development requires specific code examples. As PHP is more and more widely used, the memory overflow problem in PHP development has also become a common challenge faced by developers. Memory overflow refers to a situation where the memory requested by a program exceeds the memory space limit during operation, resulting in an exception or crash in the program. This article will introduce how to solve the memory overflow problem in PHP development and provide some specific code examples. Optimizing the code structure First, we need to optimize the code structure. A simple and high-level

Java Error: JVM memory overflow error, how to deal with and avoid Java Error: JVM memory overflow error, how to deal with and avoid Jun 24, 2023 pm 02:19 PM

Java is a popular programming language. During the development of Java applications, you may encounter JVM memory overflow errors. This error usually causes the application to crash, affecting the user experience. This article will explore the causes of JVM memory overflow errors and how to deal with and avoid such errors. What is JVM memory overflow error? The Java Virtual Machine (JVM) is the running environment for Java applications. In the JVM, memory is divided into multiple areas, including heap, method area, stack, etc. The heap is used to store created objects

How to Fix: Java Performance Error: Memory Overflow How to Fix: Java Performance Error: Memory Overflow Aug 19, 2023 am 11:05 AM

How to solve: Java Performance Error: Memory Overflow Introduction: Memory overflow (OutofMemoryError) is one of the common performance problems in Java. A memory overflow error occurs when the memory required by a program exceeds the memory space provided by the virtual machine. This article will introduce some common methods to solve memory overflow errors and provide corresponding code examples. 1. Causes of memory overflow errors 1.1 Too many objects are created In Java, each object occupies a certain amount of memory space. If Cheng

See all articles