PHPExcel下载为zip文件 [英] PHPExcel download as zip file

查看:350
本文介绍了PHPExcel下载为zip文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这段代码,可以通过PHPExcel和CI与excel文件一起很好地下载,但是我想将其作为zip文件下载,并且其中是excel文件.

I have this code and works fine downloading with excel file with PHPExcel and CI but I want to download it as zip file and inside of it is the excel file.

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

这篇关于PHPExcel下载为zip文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆