首页 > 解决方案 > 压缩问题 Excel 使用 Maatwebsite

问题描述

问题很简单 我尝试压缩文件的形式是下一个

 $zipname = 'file.zip';
 $zip = new ZipArchive;
 $zip->open($zipname, ZipArchive::CREATE);
 $zip->addFile(Excel::download(new SheetsExports($page1, $page2), $filename . '.xls'));
  $zip->close()
 echo 'Archive created!'
 header('Content-disposition: attachment; filename=files.zip');
 header('Content-type: application/zip');
 <!-- end snippet -->
 $zip->close()
 echo 'Archive created!';
 header('Content-disposition: attachment; filename=files.zip');
 header('Content-type: application/zip');

在 laravel 控制器中,我使用外部库 Maatwebsite 然后当我执行 brwoser apair 时出现下一个错误


损坏的内容错误


该网站位于http://hall.test/indiceDeRotacion?_token=2Eh6WAl0W43l8ANzLz0e4GGW9reV04ESpURvEi1H&almacen=PRINCIPAL&fechaDesde=2018-05-01&fechaHasta=2019-04-30&proveedor=3000 familia=&type=xls&compresion=on&email=


所以我需要一个解决方案,或者另一个外部库,或者我的代码中的正确方法才能工作

标签: phplaravellaravel-5maatwebsite-excel

解决方案


我用这段代码解决了这个问题

 $zip = new ZipArchive;
        if ($zip->open('yyyy.zip', ZipArchive::CREATE) === true) {
            $zip->addFile(Excel::download(new SheetsExports($page1, $page2),  $filename . '.xls')->getFile(),
                'xxxx.xls');
            $zip->close();
        }
        return response()->download("yyyy.zip");

推荐阅读