首頁 >後端開發 >php教程 >如何使用 PHPExcel 將 Excel 檔案匯出到瀏覽器下載

如何使用 PHPExcel 將 Excel 檔案匯出到瀏覽器下載

Susan Sarandon
Susan Sarandon原創
2024-10-19 08:45:02711瀏覽

How to Export Excel Files to Browser Downloads Using PHPExcel

使用PHPExcel 將Excel 檔案匯出到客戶端下載

使用PHPExcel 產生Excel 檔案時,通常需要提供以下選項:使用者下載這些文件而不將它們保存在伺服器上。或者,您可能想要在下載完成後刪除檔案。

要繞過將文件保存在伺服器上並將其直接重定向到客戶端瀏覽器進行下載,請使用php://output 流作為保存目標:

<code class="php">$objWriter->save('php://output');</code>

此外,設定正確的標頭對於文件下載至關重要:

<code class="php">// Set the MIME type for an Excel file
header('Content-type: application/vnd.ms-excel');

// Specify the filename for the downloaded Excel file
header('Content-Disposition: attachment; filename="file.xls"');

// Write the Excel file to the browser
$objWriter->save('php://output');</code>

以上是如何使用 PHPExcel 將 Excel 檔案匯出到瀏覽器下載的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn