Hướng dẫn phpspreadsheet download file without saving - tệp tải xuống phpspreadsheet mà không lưu

Tôi đang sử dụng phpspreadsheet để tạo tệp excel trong symfony 4. Mã của tôi là:

$spreadsheet = $this->generateExcel($content);

$writer = new Xlsx($spreadsheet);
$filename = "myFile.xlsx";
$writer->save($filename); // LINE I WANT TO AVOID
$response = new BinaryFileResponse($filename);
$response->headers->set('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
$response->setContentDisposition(
    ResponseHeaderBag::DISPOSITION_ATTACHMENT,
    $filename
);

Nhưng tôi không muốn lưu tệp và sau đó đọc nó để trả lại cho người dùng. Tôi muốn tải xuống nội dung excel trực tiếp. Có cách nào để làm điều đó?

Tôi đã tìm kiếm cách tạo một luồng nội dung (như câu trả lời này nói) nhưng tôi đã không thành công.

Cảm ơn trước và xin lỗi về tiếng Anh của tôi

Đã hỏi ngày 22 tháng 6 năm 2018 lúc 18:44Jun 22, 2018 at 18:44

Hướng dẫn phpspreadsheet download file without saving - tệp tải xuống phpspreadsheet mà không lưu

GenaritogenaritoGenarito

2.7354 huy hiệu vàng27 Huy hiệu bạc47 Huy hiệu đồng4 gold badges27 silver badges47 bronze badges

Theo tôi hiểu, bạn đang tạo nội dung trong mã của mình. Bạn có thể truyền phát phản hồi trong Symfony và định cấu hình người viết phpspreadsheet để lưu vào 'php: // output' (xem tại đây, đầu ra chuyển hướng tài liệu chính thức đến trình duyệt web của khách hàng).

Dưới đây là một ví dụ hoạt động bằng cách sử dụng Symfony 4.1 và PHPSPREADSHEET 1.3:

getActiveSheet();
        $sheet->setCellValue('A1', 'Hello World !');

        $writer = new Writer\Xls($spreadsheet);

        $response =  new StreamedResponse(
            function () use ($writer) {
                $writer->save('php://output');
            }
        );
        $response->headers->set('Content-Type', 'application/vnd.ms-excel');
        $response->headers->set('Content-Disposition', 'attachment;filename="ExportScan.xls"');
        $response->headers->set('Cache-Control','max-age=0');
        return $response;
    }
}

Hướng dẫn phpspreadsheet download file without saving - tệp tải xuống phpspreadsheet mà không lưu

CID

Phù vàng 14.6K4 Huy hiệu vàng26 Huy hiệu đồng4 gold badges26 silver badges44 bronze badges

Đã trả lời ngày 3 tháng 8 năm 2018 lúc 14:46Aug 3, 2018 at 14:46

Hướng dẫn phpspreadsheet download file without saving - tệp tải xuống phpspreadsheet mà không lưu

2

Đây là giải pháp cho Laravel. Nhưng cuối cùng nó vẫn sử dụng Symfony\Component\HttpFoundation\StreamedResponse.

        $contentDisposition = 'attachment; filename="' . $fileName . '"';
        $contentType = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';

        $response = response()->streamDownload(function () use ($spreadsheet) {
            $writer = new Xlsx($spreadsheet);
            $writer->save('php://output');
        });
        $response->setStatusCode(200);
        $response->headers->set('Content-Type', $contentType);
        $response->headers->set('Content-Disposition', $contentDisposition);

Sau đó bạn có thể gửi phản hồi trực tiếp dưới dạng luồng với

$response->send();

hoặc chỉ trả lại nó trong bộ điều khiển

return $response;

Đã trả lời ngày 19 tháng 3 năm 2021 lúc 13:48Mar 19, 2021 at 13:48

Oleg Abrazhaevoleg AbrazhaevOleg Abrazhaev

2.6432 huy hiệu vàng27 Huy hiệu bạc40 Huy hiệu đồng2 gold badges27 silver badges40 bronze badges

Lưu nút văn bản, không có dữ liệu;


	
		PHP Excel
	
	
		


getActiveSheet(); $count = $sheet->getHighestDataRow(); $writer = new Writer\Xls($spreadsheet); for($i=1;$i<=$count;$i++){ $nome = $sheet->getCell('A'.$i)->getValue(); $telefone = $sheet->getCell('B'.$i)->getValue(); if( $telefone == "" || strpos($nome, "Nome do Cliente") === true || strpos($nome, "Total") === true ){ $sheet->removeRow($count); } if( strpos($nome, "Nome do Cliente") === false && strpos($nome, "Total") === false && $telefone != "" ){ $nome = explode(" ", $nome); $telefone = str_replace(['-',' ','-','(',')'],'',$telefone); if(strlen($telefone) < 11){ $telefone = substr_replace($telefone, '9', 2, -strlen($telefone)); } $sheet->setCellValue('A'.$i, $nome[0]); $sheet->setCellValue('B'.$i, $telefone); } } // $writer = new Xlsx($spreadsheet); // $writer = IOFactory::createWriter($spreadsheet, 'Xls'); // $writer->save("Contatos-.xls"); $streamedResponse = new StreamedResponse(); $streamedResponse->setCallback(function () use ($spreadsheet) { // $spreadsheet = //create you spreadsheet here; $writer = new Xlsx($spreadsheet); $writer->save('php://output'); }); $streamedResponse->setStatusCode(200); $streamedResponse->headers->set('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); $streamedResponse->headers->set('Content-Disposition', 'attachment; filename="your_file.xls"'); return $streamedResponse->send(); } ?>


Hướng dẫn phpspreadsheet download file without saving - tệp tải xuống phpspreadsheet mà không lưu