创建一个临时文件 excel 并使用 zip in PHP 批量下载

Create a temp excel & bulk download all with zip in PHP

我想将我的大型数据库数据下载为 excel。 但是由于数据量太大,感觉下载不了,服务器负担大,处理时间长,还经常崩溃。

现在,我想创建多个临时 excel 文件 'on the go' 一些有限的数据 Ex: 50000 rows of data & 在数据的末尾我想下载所有这些临时文件 Zip . 因此,它不会加载服务器并防止它崩溃。

是否可以通过 PHP-CodeIgniter 实现。有人可以指导我吗?

您可以通过多种方式做到这一点。

  1. 您认为将其压缩到一个文件夹中
  2. 增加 CI 或服务器中的执行限制和内存限制。
  3. 实施队列服务

压缩你可以按下面的方式做

<?php
  
// Enter the name of directory
$pathdir = "DirectoryName/"; 
$zipcreated = "NameOfZip.zip";

$zip = new ZipArchive;
   
if($zip -> open($zipcreated, ZipArchive::CREATE ) === TRUE) {
      
    // Store the path into the variable
    $dir = opendir($pathdir);
       
    while($file = readdir($dir)) {
        if(is_file($pathdir.$file)) {
            $zip -> addFile($pathdir.$file, $file);
        }
    }
    $zip ->close();
}
  
?>

增加选项:

您可以在 PHPExcel 中使用 excel5。它可以处理 65536 行。为此

ini_set("max_execution_time", 'time_limit'); //see manual
ini_set('memory_limit', 'memory_limit'); //your memory limit as string

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');

之后按照 PHPExcel 的文档进行操作。

对于队列选项: 这是一个有点复杂的过程。 您可以实现队列服务并将生成 excel 的责任交给队列服务。生成后,可以通知用户或return下载URLexcel文件。 对于通知,您还需要实现通知服务。