后台导出 大数据 数据量过大生成压缩包

function getExcelZip($fileName, $headArr, $data, m s g = ′ ′ , msg = '', msg=,special_key){
//对数据进行检验
if (empty( d a t a ) ∣ ∣ ! i s a r r a y ( data) || !is_array( data)!isarray(data)) {
die(“data must be a array”);
}

//检查文件名
if (empty($fileName)) {
    die("filename must be existed");
}

//获取总列数
$totalColumn = count($headArr);
$charColumn = chr($totalColumn + 64);
$date = date("Y-m-d", time());
$fileName .= "_{$date}";


$new_data = array_chunk($data,100);
$zip_name = "uploads/export/export.zip";
$zip = new \ZipArchive();
$zip->open($zip_name,\ZipArchive::CREATE);   //打开压缩包
$file = [];
foreach($new_data as $k=>$val){
    //创建PHPExcel对象
    $objPHPExcel = new \PHPExcel();

    $objPHPExcel->setActiveSheetIndex(0);    //设置当前的sheet  操作第一个工作表
    $objActSheet = $objPHPExcel->getActiveSheet();    //添加数据
    $phpstyle = new \PHPExcel_Style_Color();

    //表头变颜色
    $objActSheet->getStyle('A1:' . $charColumn . '1')->getFont()->getColor()->setARGB($phpstyle::COLOR_BLUE);    //设置颜色

    //设置批注
    if (!empty($msg)) {
        $objActSheet->getStyle('A2')->getFont()->getColor()->setARGB($phpstyle::COLOR_RED);
        $objActSheet->setCellValue('A2', $msg);    //给单个单元格设置内容
        $objActSheet->mergeCells('A2:' . $charColumn . '2');    //合并单元格
    }

    //设置表头
    $key = ord("A");
    foreach ($headArr as $v) {
        $colum = chr($key);
        $objActSheet->setCellValue($colum . '1', $v);
        $objActSheet->getColumnDimension($colum)->setWidth(20);
        $key++;
    }

    //写入数据
    if (!empty($msg)) {
        $column = 3;
    } else {
        $column = 2;
    }
    foreach ($val as $key => $rows) {     //行写入
        $span = ord("A");
        foreach ($rows as $keyName => $value) {    // 列写入
            $j = chr($span);
            if($keyName == $special_key && empty(!$value)){
                $objActSheet->getRowDimension($column)->setRowHeight(60);    //设置行高
                $objDrawing = new \PHPExcel_Worksheet_Drawing();
                $objDrawing->setPath($value);
                $objDrawing->setWidth(180);
                $objDrawing->setHeight(80);
                $objDrawing->setCoordinates($j . $column);
                $objDrawing->setWorksheet($objPHPExcel->getActiveSheet());
            }else{
                if ($keyName !== 'img') {
                    $objActSheet->setCellValue($j . $column, $value);
                } elseif ($keyName == 'img') {
                    $objActSheet->getRowDimension($column)->setRowHeight(60);    //设置行高
                    $objDrawing = new \PHPExcel_Worksheet_Drawing();
                    $objDrawing->setPath($value);
                    $objDrawing->setWidth(50);
                    $objDrawing->setHeight(50);
                    $objDrawing->setCoordinates($j . $column);
                    $objDrawing->setWorksheet($objPHPExcel->getActiveSheet());
                }
            }

            $span++;
        }
        $column++;
    }

    $zip->addFile('uploads/export/'.$key.$fileName,basename('uploads/export/'.$key.$fileName));
    header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
    header("Content-Disposition: attachment;filename=\"$fileName\"");
    header('Cache-Control: max-age=0');
    $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
    $objWriter->save('uploads/export/'.$k.'.xls'); //文件通过浏览器下载
    $zip->addFile('./uploads/export/'.$k.'.xls',basename('./uploads/export/'.$k.'.xls'));
    array_unshift($file,'./uploads/export/'.$k.'.xls');

// @unlink(’./uploads/export/’.$k.’.xls’);

}
$zip->close();
$file_name = "/export.zip";
$file_dir = './uploads/export';

// dump( f i l e d i r . file_dir. filedir.file_name);die;
if (!file_exists ( $file_dir . $file_name )) {
header(‘HTTP/1.1 404 NOT FOUND’);
} else {
$dfile = $file_dir . f i l e n a m e ; / / 下 载 文 件 o b c l e a n ( ) ; h e a d e r ( ′ P r a g m a : p u b l i c ′ ) ; h e a d e r ( ′ L a s t − M o d i f i e d : ′ . g m d a t e ( ′ D , d M Y H : i : s ′ ) . ′ G M T ′ ) ; h e a d e r ( ′ C a c h e − C o n t r o l : n o − s t o r e , n o − c a c h e , m u s t − r e v a l i d a t e ′ ) ; h e a d e r ( ′ C a c h e − C o n t r o l : p r e − c h e c k = 0 , p o s t − c h e c k = 0 , m a x − a g e = 0 ′ ) ; h e a d e r ( ′ C o n t e n t − T r a n s f e r − E n c o d i n g : b i n a r y ′ ) ; h e a d e r ( ′ C o n t e n t − E n c o d i n g : n o n e ′ ) ; h e a d e r ( ′ C o n t e n t − t y p e : m u l t i p a r t / f o r m − d a t a ′ ) ; h e a d e r ( ′ C o n t e n t − D i s p o s i t i o n : a t t a c h m e n t ; f i l e n a m e = " ′ . file_name; // 下载文件 ob_clean(); header('Pragma: public'); header('Last-Modified:' . gmdate('D, d M Y H:i:s') . 'GMT'); header('Cache-Control:no-store, no-cache, must-revalidate'); header('Cache-Control:pre-check=0, post-check=0, max-age=0'); header('Content-Transfer-Encoding:binary'); header('Content-Encoding:none'); header('Content-type:multipart/form-data'); header('Content-Disposition:attachment; filename="'. filename;//obclean();header(Pragma:public);header(LastModified:.gmdate(D,dMYH:i:s).GMT);header(CacheControl:nostore,nocache,mustrevalidate);header(CacheControl:precheck=0,postcheck=0,maxage=0);header(ContentTransferEncoding:binary);header(ContentEncoding:none);header(Contenttype:multipart/formdata);header(ContentDisposition:attachment;filename=".fileName.’.zip"’); //设置下载的默认文件名
header(‘Content-length:’ . filesize($dfile));
f p = f o p e n ( fp = fopen( fp=fopen(dfile, ‘r’);
while (connection_status() == 0 && b u f = @ f r e a d ( buf = @fread( buf=@fread(fp, 8192)) {
echo KaTeX parse error: Expected 'EOF', got '}' at position 14: buf; }̲ fclose…fp);
@unlink( d f i l e ) ; @ f l u s h ( ) ; @ o b f l u s h ( ) ; f o r e a c h ( dfile); @flush(); @ob_flush(); foreach( dfile);@flush();@obflush();foreach(file as k 1 = > k1=> k1=>val1){
@unlink($val1);
}
exit();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值