phpExcel导出大量数据出现内存溢出错误的解决方法_PHP
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来导出数据,上面方法简单给了个流程,具体你自己补充!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



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

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.

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.

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