Php 如何在单个zip文件中保存多个excel文件

Php 如何在单个zip文件中保存多个excel文件,php,codeigniter,phpexcel,Php,Codeigniter,Phpexcel,我的excel文件代码如下所示 $dte=date('d-m-Y',strtotime($dt)); $fname='Logsheet_'.$dte.'_Zone_' .$zone_id.'.xls'; $filename='Backup.xls'; //save our workbook as this file name header('Content-Type: application/vnd.ms-excel'); //mime type header('Content-Disposit

我的excel文件代码如下所示

$dte=date('d-m-Y',strtotime($dt));
$fname='Logsheet_'.$dte.'_Zone_' .$zone_id.'.xls';
$filename='Backup.xls'; //save our workbook as this file name
header('Content-Type: application/vnd.ms-excel'); //mime type
header('Content-Disposition: attachment;filename="'.$fname.'"'); //tell browser what's the file name
header('Cache-Control: max-age=0'); //no cache
//save it to Excel5 format (excel 2003 .XLS file), change this to 'Excel2007' (and adjust the filename extension, also the header mime type)
//if you want to save it as .XLSX Excel 2007 format
$objWriter = PHPExcel_IOFactory::createWriter($this->excel, 'Excel5');  
//force user to download the Excel file without writing it to server's HD
$objWriter->save('php://output');

然后我想知道,如何更改此代码,因为我可以将多个excel文件放在一个zip文件中作为输出。

好的。我认为你不是一个专业的谷歌搜索者,我以前也这么做过,但我不会给你完整的解决方案。在PHP中有一个名为
ZipArchive
的类。它在服务器上创建zip文件。它很容易使用。网址是。基本工作流程是:

  • 将excel文件保存到服务器

  • 将这些文件放入数组中

  • 通过
    ZipArchive
  • foreach文件数组并将它们添加到zip文件中
  • 保存zip文件
  • 将zip文件发送到header
ZipArchive
有很好的文档记录,我相信您会在那里找到解决方案


祝你今天愉快

好的……问题出在哪里?到目前为止,您尝试了什么?首先保存所有文件,将其压缩成一个文件,并提供下载。在磁盘上创建一个临时文件夹;将每个电子表格文件保存到该文件夹(使用不同的名称);打开一个新的zip文件,使用
addGlob()
将所有文件添加到zip,关闭zip并将其发送到用户浏览器;删除临时文件夹及其内容。。。。有关如何为文件夹的所有内容创建zip文件的信息,请参见