-2

My excel file code is as below...

$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');

And then I want to know, how can I change this code, as I can put multiple excel files, into a single zip file as output.

Ali Lotfi
  • 856
  • 3
  • 18
  • 40
rohit
  • 1
  • 1
  • 2
  • Ok ...and where is the Problem ? What have you tried so far ? – invidicult Jan 12 '15 at 12:22
  • 1
    first save all your files, zip it into 1 file, and offer it to download. – vaso123 Jan 12 '15 at 12:24
  • 2
    Create a temporary folder on disk; save each spreadsheet file to that folder (with a different name); open a new zip file, use `addGlob()` to add all the files to the zip, close the zip and send it to user's browser; delete temporary folder and its contents.... see http://stackoverflow.com/questions/4914750/how-to-zip-a-whole-folder-using-php/4914894#4914894 for how to create a zip file for all the contents of a folder – Mark Baker Jan 12 '15 at 12:26

1 Answers1

1

Ok. I think you are not a professional googler and I did this before but I won't give you the whole solution. In PHP there is a class named ZipArchive. It creates zip files on the server. It's very easy to use. The url is here. The basic workflow is:

  • you save the excel file to the server

  • get those files into an array

  • create an empty zip via ZipArchive
  • foreach the array of files and add them to the zip file
  • Save the zip file
  • Send the zip file to header.

ZipArchive is pretty well documented and I'm sure you will find the solution there.

Have a nice day!

Krisztián Dudás
  • 856
  • 10
  • 22