PHPExcel下载为zip文件

PHPExcel下载为zip文件,php,codeigniter,zip,phpexcel,Php,Codeigniter,Zip,Phpexcel,我有这段代码,可以很好地下载带有PHPExcel和CI的excel文件,但我想下载它作为zip文件,里面是excel文件 $header = array( 'foodgroup','energy','protein','fat','cho','calcium','iron','thiamin','niacin','vitamin_c' ,'vitamin_a', ); require_once APPPATH."/third_party/PHPExcel.

我有这段代码,可以很好地下载带有PHPExcel和CI的excel文件,但我想下载它作为zip文件,里面是excel文件

    $header = array(
    'foodgroup','energy','protein','fat','cho','calcium','iron','thiamin','niacin','vitamin_c'
    ,'vitamin_a',
    );
    require_once APPPATH."/third_party/PHPExcel.php";
    $sheet = new PHPExcel();
    $file = $this->dietary_model->getById($subject_id,'dietary_subject');
    $filename = $file->name;
    $this->load->helper('date');
    $date = date('Y-m-d'); 
    //1st Sheet
    $users = $this->dietary_model->getdata($subject_id,'1');
    $sheet->setActiveSheetIndex(0);
    $activeSheet = $sheet->getActiveSheet();
    $activeSheet->setTitle('Day 1');
    $activeSheet->getStyle('A1:T1')->getFont()->setBold(true);
    $activeSheet->fromArray($header, null, 'A1');
    $activeSheet->fromArray($users,null, 'A2');

    //2nd Sheet
    $users2 = $this->dietary_model->getdata($subject_id,'2');
    $sheet->createSheet();
    $sheet->setActiveSheetIndex(1);
    $activeSheet2 = $sheet->getActiveSheet(1);
    $activeSheet2->setTitle('Day 2');
    $activeSheet2->getStyle('A1:T1')->getFont()->setBold(true);
    $activeSheet2->fromArray($header, null, 'A1');
    $activeSheet2->fromArray($users2,null, 'A2');


    header('Content-Type: application/vnd.ms-excel');
    header('Content-Disposition: attachment;filename='.$filename.' '.$date.'.xlsx'); 
    header('Cache-Control: max-age=0');
    $objWriter = PHPExcel_IOFactory::createWriter($sheet, 'Excel2007');  
    echo '<script>console.log('.$objWriter.')</script>';
    exit;
$header=数组(
“食物组”、“能量”、“蛋白质”、“脂肪”、“cho”、“钙”、“铁”、“硫胺素”、“烟酸”、“维生素c”
“维生素a”,
);
需要一次APPPATH。“/third\u party/PHPExcel.php”;
$sheet=new PHPExcel();
$file=$this->dietary\u model->getById($subject\u id,'dietary\u subject');
$filename=$file->name;
$this->load->helper('date');
$date=日期('Y-m-d');
//第一页
$users=$this->dietary_model->getdata($subject_id,'1');
$sheet->setActiveSheetIndex(0);
$activeSheet=$sheet->getActiveSheet();
$activeSheet->setTitle(“第1天”);
$activeSheet->getStyle('A1:T1')->getFont()->setBold(true);
$activeSheet->fromArray($header,null,'A1');
$activeSheet->fromArray($users,null,'A2');
//第二页
$users2=$this->dietary_model->getdata($subject_id,'2');
$sheet->createSheet();
$sheet->setActiveSheetIndex(1);
$activeSheet2=$sheet->getActiveSheet(1);
$activeSheet2->setTitle(“第2天”);
$activeSheet2->getStyle('A1:T1')->getFont()->setBold(true);
$activeSheet2->fromArray($header,null,'A1');
$activeSheet2->fromArray($users2,null,'A2');
标题('Content-Type:application/vnd.ms-excel');
标题('Content-Disposition:attachment;filename='.$filename.'.$date..xlsx');
标头('Cache-Control:max age=0');
$objWriter=PHPExcel_IOFactory::createWriter($sheet,'Excel2007');
echo'console.log('.$objWriter.');
出口
试试这个:

$header = array(
    'foodgroup','energy','protein','fat','cho','calcium','iron','thiamin','niacin','vitamin_c'
    ,'vitamin_a',
    );
require_once APPPATH."/third_party/PHPExcel.php";
$sheet = new PHPExcel();
$file = $this->dietary_model->getById($subject_id,'dietary_subject');
$filename = $file->name;
$this->load->helper('date');
$date = date('Y-m-d'); 
//1st Sheet
$users = $this->dietary_model->getdata($subject_id,'1');
$sheet->setActiveSheetIndex(0);
$activeSheet = $sheet->getActiveSheet();
$activeSheet->setTitle('Day 1');
$activeSheet->getStyle('A1:T1')->getFont()->setBold(true);
$activeSheet->fromArray($header, null, 'A1');
$activeSheet->fromArray($users,null, 'A2');
 //2nd Sheet
$users2 = $this->dietary_model->getdata($subject_id,'2');
$sheet->createSheet();
$sheet->setActiveSheetIndex(1);
$activeSheet2 = $sheet->getActiveSheet(1);
$activeSheet2->setTitle('Day 2');
$activeSheet2->getStyle('A1:T1')->getFont()->setBold(true);
$activeSheet2->fromArray($header, null, 'A1');
$activeSheet2->fromArray($users2,null, 'A2');
$objWriter = PHPExcel_IOFactory::createWriter($sheet, 'Excel2007');  

$excel_file_tmp = tempnam("/tmp", 'your_prefix');
$objWriter->save($excel_file_tmp);

//zip
$zip_file_tmp = tempnam("/tmp", 'your_prefix');
$zip        = new ZipArchive();
$zip->open($zip_file_tmp, ZipArchive::OVERWRITE);
$zip->addFile($excel_file_tmp, 'your_name.xlsx');
$zip->close();

//download
$download_filename = 'your_name.zip'; 
header("Content-Type: application/zip");
header("Content-Length: " . filesize($zip_file_tmp));
header("Content-Disposition: attachment; filename=\"" . $download_filename . "\"");
readfile($zip_file_tmp);
unlink($excel_file_tmp);
unlink($zip_file_tmp);

将其写入服务器上的一个文件,然后将其添加为zip文件并将zip文件发送到php://output 有了合适的标题感谢马克·贝克尔感谢阿尔卡迪乌斯G.,我使用了CI的zip库