在 Laravel 4.2 中使用 PHPExcel 生成报告的解决方案
Solution to generate reports with PHPExcel in Laravel 4.2
我在 Laravel 4.2 中使用 PHPExcel 生成报告时遇到问题,从数据库中获取的行数非常大(超过 50 万行),因此数组创建使用大量 PHP 内存并在生成最终文件时完成导致内存溢出。我尝试使用 PHPExcel 缓存系统,但它也使用了大量系统内存。我可能正在使用其他解决方案吗?我考虑过以部分方式处理记录的创建,例如一次处理 5000 行,但我不知道这样的努力是否能解决我的问题。
我用来使用PHPExcel缓存(discISAM)的代码:
public function __construct() {
$templateFileName = $this->getTemplateFileName();
$fullPathTemplate = public_path().'/Temp/'.$templateFileName.'.xlsx';
// $cacheMethod = \PHPExcel_CachedObjectStorageFactory::cache_to_phpTemp;
$cacheMethod = \PHPExcel_CachedObjectStorageFactory::cache_to_discISAM;
$cacheSettings = array(
// 'memoryCacheSize' => '150MB'
'dir' => 'public/Temp/cache'
);
$cacheAtivo = \PHPExcel_Settings::setCacheStorageMethod($cacheMethod, $cacheSettings);
if (! $cacheAtivo) {
die($cacheMethod . " Cache invalido " . EOL);
}
$objPhpReader = new \PHPExcel_Reader_Excel2007();
$this->phpExcelObject = $objPhpReader->load($fullPathTemplate);
}
提前谢谢大家的关注
您可以选中 Chunk Method 来限制行,然后为每个块写入文件。块是为此目的制作的
Chunking Results
If you need to process a lot (thousands) of Eloquent records, using the chunk command will allow you to do without eating all of your RAM:
User::chunk(200, function($users)
{
foreach ($users as $user)
{
//
}
});
The first argument passed to the method is the number of records you wish to receive per "chunk". The Closure passed as the second argument will be called for each chunk that is pulled from the database.
我在 Laravel 4.2 中使用 PHPExcel 生成报告时遇到问题,从数据库中获取的行数非常大(超过 50 万行),因此数组创建使用大量 PHP 内存并在生成最终文件时完成导致内存溢出。我尝试使用 PHPExcel 缓存系统,但它也使用了大量系统内存。我可能正在使用其他解决方案吗?我考虑过以部分方式处理记录的创建,例如一次处理 5000 行,但我不知道这样的努力是否能解决我的问题。
我用来使用PHPExcel缓存(discISAM)的代码:
public function __construct() {
$templateFileName = $this->getTemplateFileName();
$fullPathTemplate = public_path().'/Temp/'.$templateFileName.'.xlsx';
// $cacheMethod = \PHPExcel_CachedObjectStorageFactory::cache_to_phpTemp;
$cacheMethod = \PHPExcel_CachedObjectStorageFactory::cache_to_discISAM;
$cacheSettings = array(
// 'memoryCacheSize' => '150MB'
'dir' => 'public/Temp/cache'
);
$cacheAtivo = \PHPExcel_Settings::setCacheStorageMethod($cacheMethod, $cacheSettings);
if (! $cacheAtivo) {
die($cacheMethod . " Cache invalido " . EOL);
}
$objPhpReader = new \PHPExcel_Reader_Excel2007();
$this->phpExcelObject = $objPhpReader->load($fullPathTemplate);
}
提前谢谢大家的关注
您可以选中 Chunk Method 来限制行,然后为每个块写入文件。块是为此目的制作的
Chunking Results
If you need to process a lot (thousands) of Eloquent records, using the chunk command will allow you to do without eating all of your RAM:
User::chunk(200, function($users) { foreach ($users as $user) { // } });
The first argument passed to the method is the number of records you wish to receive per "chunk". The Closure passed as the second argument will be called for each chunk that is pulled from the database.