美文网首页
php导出excel、下载图片并压缩成zip实例

php导出excel、下载图片并压缩成zip实例

作者: 村长王无敌 | 来源:发表于2020-05-26 14:20 被阅读0次

    <?php

    class ControllerFormForm{

        //导出excel

        public function export(){

            require_once '../phpexcel/PHPExcel.php';

            $this->load->model('form/form');

            $export_date = $this->request->get['export_date_added'];

            if($export_date){

                $lists = $this->model_form_form->getFormByDate($export_date);

                // 创建Excel文件对象

                $objPHPExcel = new PHPExcel();

                //根据excel坐标,添加数据

                $objPHPExcel->setActiveSheetIndex(0)

                    ->setCellValue('A1', 'Globalcash Card No. 全球付卡號')

                    ->setCellValue('B1', "Cardholder's English Name 持卡人英文姓名/拼音")

                    ->setCellValue('C1', "Cardholder's Chinese Name 持卡人中文姓名")

                    ->setCellValue('D1', "Tel. Country code电话國家區號")

                    ->setCellValue('E1', "Mobile No.手提電話號碼")

                    ->setCellValue('F1', "Address 地址")

                    ->setCellValue('G1', "Type of the certificate 證件類型")

                    ->setCellValue('H1', "Certificate No. 證件號碼")

                    ->setCellValue('I1', "Date of Issue 證件有效期起")

                    ->setCellValue('J1', "Date of expiry 證件有效期止")

                    ->setCellValue('K1', "Referrer 推薦人")

                    ->setCellValue('L1', "Nationality 國籍")

                    ->setCellValue('M1', "CDD Caegory 實名狀態")

                    ->setCellValue('N1', "Notes 備註")

                    ->setCellValue('O1', "Date of birth 出生日期");

                //设置保存版本格式  

                if($excel2007){  

                    $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);  

                    $filename = $export_date.'export.xlsx';  

                }else{  

                    $objWriter = new PHPExcel_Writer_Excel5($objPHPExcel);  

                    $filename = $export_date.'export.xls';  

                }  

                //接下来就是写数据到表格里面去  

                $objActSheet = $objPHPExcel->getActiveSheet(); 

                foreach ($lists as $key=>$value) {  

                    $key = $key+2;

                    $objActSheet->setCellValue('A'.$key,$value['card_number']);

                    $objActSheet->setCellValue('B'.$key,$value['english_name']);

                    $objActSheet->setCellValue('C'.$key,$value['chinese_name']);

                    $objActSheet->setCellValue('D'.$key,$value['tel_code']);

                    $objActSheet->setCellValue('E'.$key,$value['telephone']);

                    $objActSheet->setCellValue('F'.$key,$value['address']);

                    $objActSheet->setCellValue('G'.$key,$value['certificate_type']);

                    $objActSheet->setCellValue('H'.$key,$value['certificate_number']);

                    $objActSheet->setCellValue('I'.$key,$value['certificate_start_date']);

                    $objActSheet->setCellValue('J'.$key,$value['certificate_end_date']);

                    $objActSheet->setCellValue('K'.$key,$value['referrer']);

                    $objActSheet->setCellValue('L'.$key,$value['country']);

                    $objActSheet->setCellValue('M'.$key,$value['name_status']);

                    $objActSheet->setCellValue('N'.$key,$value['notes']);

                    $objActSheet->setCellValue('O'.$key,$value['birth_date']);    

                }  

                // 下载这个表格,在浏览器输出  

                header("Pragma: public");  

                header("Expires: 0");  

                header("Cache-Control:must-revalidate, post-check=0, pre-check=0");  

                header("Content-Type:application/force-download");  

                header("Content-Type:application/vnd.ms-execl");  

                header("Content-Type:application/octet-stream");  

                header("Content-Type:application/download");;  

                header('Content-Disposition:attachment;filename='.$filename.'');  

                header("Content-Transfer-Encoding:binary");  

                $objWriter->save('php://output');  

            }

        }

        //批量下载图片zip

        public function picDownload($images,$date){

            $filename = "../image/submission_report/".$data."/".date ('Ymd').".zip";

            // 生成文件

            $zip = new \ZipArchive ();

            // 使用本类,linux需开启zlib,windows需取消php_zip.dll前的注释

            if ($zip->open ($filename ,\ZipArchive::OVERWRITE) !== true) {

                //OVERWRITE 参数会覆写压缩包的文件 文件必须已经存在

                if($zip->open ($filename ,\ZipArchive::CREATE) !== true){

                    // 文件不存在则生成一个新的文件 用CREATE打开文件会追加内容至zip

                    exit ('无法打开文件,或者文件创建失败');

                }

            }

            foreach($images as $key => $v){

                $arr1=explode('/', $v['file1']);

                $arr2=explode('/', $v['file2']);

                $arr3=explode('/', $v['file3']);

                $arr4=explode('/', $v['file4']);

                $arr5=explode('/', $v['file5']);

                $arr6=explode('/', $v['file6']);

                $arr7=explode('/', $v['file7']);

                $arr8=explode('/', $v['file8']);

                if(file_exists($v['file1'])){

                    $zip->addFile($v['file1'],basename($arr1['4']));

                }

                if(file_exists($v['file2'])){

                    $zip->addFile($v['file2'],basename($arr2['4']));

                }

                if(file_exists($v['file3'])){

                    $zip->addFile($v['file3'],basename($arr3['4']));

                }

                if(file_exists($v['file4'])){

                    $zip->addFile($v['file4'],basename($arr4['4']));

                }

                if(file_exists($v['file5'])){

                    $zip->addFile($v['file5'],basename($arr5['4']));

                }

                if(file_exists($v['file6'])){

                    $zip->addFile($v['file6'],basename($arr6['4']));

                }

                if(file_exists($v['file7'])){

                    $zip->addFile($v['file7'],basename($arr7['4']));

                }

                if(file_exists($v['file8'])){

                    $zip->addFile($v['file8'],basename($arr8['4']));

                } 

            }

            // 关闭

            $zip->close ();

            //下面是输出下载;

            header ( "Cache-Control: max-age=0" );

            header ( "Content-Description: File Transfer" );

            header ( 'Content-disposition: attachment; filename='.basename($filename));

            header ( "Content-Type: application/zip" ); // zip格式的

            header ( "Content-Transfer-Encoding: binary" ); // 告诉浏览器,这是二进制文件

            header ( 'Content-Length: '.filesize($filename)); // 告诉浏览器,文件大小

            @readfile ( $filename );//输出文件;

            @unlink($filename);

        }

    }

    相关文章

      网友评论

          本文标题:php导出excel、下载图片并压缩成zip实例

          本文链接:https://www.haomeiwen.com/subject/niamahtx.html