如何将多个Excel文件保存在单个ZIP文件中


how to save multiple excel files in single zip file

我的 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创建空 zip
  • 每个文件数组并将它们添加到 zip 文件中
  • 保存压缩文件
  • 将 zip 文件发送到标头。

ZipArchive有据可查,我相信你会在那里找到解决方案。

有好的一天!