php 导出excel压缩包

来源:互联网 发布:大数据存储技术研究 编辑:程序博客网 时间:2024/06/05 16:15
//创建文件夹或清除文件夹下文件$dir="var/templet";if (!file_exists($dir)){    mkdir ($dir);//创建文件夹}else{    $dh=opendir($dir);    while ($file=readdir($dh))    {        if($file!="." && $file!="..")        {            $fullpath=$dir."/".$file;            unlink($fullpath);        }    }    closedir($dh);}//导出excel
//导出单个品类码excel            $oe = new PHPExcel();            $i = 0;            $x = 1;            $oe->setActiveSheetIndex(0);            $oe->getActiveSheet()->setTitle('品类码'.$category_code);            $oe->getActiveSheet()->setCellValue('A1', '品类码:'.$category_code);            $oe->getActiveSheet()->setCellValue('A2',$manage_full_name);//            $oe->getActiveSheet()->setCellValue('B1', $manage_code);            $oe->getActiveSheet()->getColumnDimension('A')->setWidth(50);//            $oe->getActiveSheet()->getColumnDimension('B')->setWidth(50);            foreach ($excel_data as $kk=>$data){                $oe->addSheet(new PHPExcel_Worksheet($oe, $kk));                $i += 1;            }            foreach ($excel_data as $kk=>$data){                $oe->setActiveSheetIndex($x);                $column = "A";                $column++;                $column++;                foreach ($data as $j=>$template_field) {                    $column++;                    $oe->getActiveSheet()->setCellValue('A1', 'ERPSKU');                    $oe->getActiveSheet()->setCellValue('B1', 'Account');                    $oe->getActiveSheet()->setCellValue('C1', 'SellerSku');                    $oe->getActiveSheet()->getColumnDimension($column)->setWidth(20);                    if($template_field['field_type'] == 4){                        $oe->getActiveSheet()->getStyle($column)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_PERCENTAGE_00);                    }                    $oe->getActiveSheet()->setCellValue($column . '1', $template_field['template_name']);                    $oe->getActiveSheet()->getColumnDimension($column)->setWidth(20);                    $oe->getActiveSheet()->getComment( $column . '1')->setAuthor('PHPExcel' );     //设置作者                    $oe->getActiveSheet()->getComment($column .'1' )->getText()->                    createTextRun($template_field['template_description']);                    $oe->getActiveSheet()->getComment( $column . '1' )->setWidth('150pt' )->setHeight('150pt');                }
             $currentRow = 1;                foreach($rowData as $rowSet=>$row){                    $column2="A";                    $currentColumn = 0 ;                    foreach ($row as $order=>$item){//                        $oe->getActiveSheet()->setCellValue(chr($colbaseIndex+$currentColumn) . ($currentRow+1), $item);                        $oe->getActiveSheet()->setCellValue($column2 . ($currentRow+1), $item);                        $column2++;                        $currentColumn++;                    }                    $currentRow++;                }                $x += 1;            }//            var_dump($erpskus[$category_code]);//            exit();            $objWriter = PHPExcel_IOFactory::createWriter($oe, "Excel2007");            $file ='var/templet/'.$line_name.'_'.$category_code.'.xlsx';            $objWriter->save($file);            $oe->disconnectWorksheets();            unset($oe);//打包
//zip打包$dir='var/templet';$datalist = array();if (is_dir($dir)){    $file_dir = scandir($dir);    foreach($file_dir as $file){        if ($file == '.' || $file == '..'){            continue;        }else{            array_push($datalist, $dir."/".$file);        }    }}//$datalist 就是指 导出的excel的数量,数量为1的时,则以Excel格式导出if (count($datalist) == 1 ){    header("Location: /$datalist[0]");    exit;}//$filename = "var/templet/templet.zip"; //最终生成的文件名(含路径)$filename = "var/templet/".$line_name."_templet.zip"; //最终生成的文件名(含路径)//生成文件$zip = new ZipArchive();//使用本类,linux需开启zlib,windows需取消php_zip.dll前的注释if ($zip->open($filename, ZIPARCHIVE::CREATE)!==TRUE) {    exit('无法打开文件,或者文件创建失败');}foreach( $datalist as $val){    if(file_exists($val)){        $zip->addFile( $val, basename($val));//第二个参数是放在压缩包中的文件名称,如果文件可能会有重复,就需要注意一下    }}$zip->close();//关闭if(!file_exists($filename)){    exit("无法找到文件"); //即使创建,仍有可能失败。。。。}header("Location: /$filename");exit;// end zip打包






原创粉丝点击