Phpspreadsheet禁用“另存为”弹出窗口进行下载

时间:2019-06-20 01:12:39

标签: php laravel-5 phpspreadsheet

我在laravel中使用phpspreadsheet包来生成excel文件,到目前为止,它的工作原理就像魅力。这是我的问题,我希望将excel保存在某些目录中而不是弹出式下载中,我给出了一个路径以保存在某些目录中,并且工作正常。

$writer->save($full_path.$filename);

但是仍然弹出弹出窗口,下面是生成excel的代码:

use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Style\Aligment;
use SpreadsheetReader;

public function export()
{
    $spreadsheet = new Spreadsheet();

    // Add some data
    $sheet = $spreadsheet->getActiveSheet();
    $sheet->getStyle('C')->getAlignment()->setHorizontal('left');
    $sheet->getColumnDimension('A')->setWidth(20);
    $sheet->getColumnDimension('B')->setWidth(20);
    $sheet->setCellValue('A1', 'Name');
    $sheet->setCellValue('B1', 'Service');

    $users = User::get();
    if(count($users) > 0) {
        $rowCount = 2;
        foreach($users as $user) {
            $sheet->setCellValue('A' . $rowCount, $user->name);
            $sheet->setCellValue('B' . $rowCount, $user->service);
            $rowCount++;
        }
    }

    $spreadsheet->getActiveSheet()->setTitle('User Services');
    $spreadsheet->setActiveSheetIndex(0);

    $filename = "file-".date('YmdHis').".xlsx";
    $full_path = public_path().'\rpts\\';

    header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
    header('Content-Disposition: attachment;filename="'.$filename.'"');
    header('Cache-Control: max-age=0');

    $writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
    //$writer->save('php://output');
    $writer->save($full_path.$filename);

    exit;
}

在导出过程中是否禁止save as弹出窗口?

enter image description here

0 个答案:

没有答案