如何使用Hyperf框架進行Excel導入導出
摘要:本文將介紹如何在Hyperf框架中實作Excel檔案的匯入和匯出功能,並給出了具體的程式碼範例。
關鍵字:Hyperf框架、Excel導入、Excel導出、程式碼範例
導入
首先,我們需要確保專案中安裝了phpoffice/phpspreadsheet
這個函式庫。可以透過在終端機中執行以下命令進行安裝:
composer require phpoffice/phpspreadsheet
- #建立控制器和路由
首先,我們需要建立一個控制器來處理導入的邏輯。在app/Controller
目錄下建立一個ExcelController.php
文件,加入以下程式碼:
<?php declare(strict_types=1); namespace AppController; use PhpOfficePhpSpreadsheetIOFactory; class ExcelController { public function import() { $uploadedFile = $this->request->file('excel_file'); $spreadsheet = IOFactory::load($uploadedFile->getPathname()); $worksheet = $spreadsheet->getActiveSheet(); $data = $worksheet->toArray(); // 处理导入逻辑 } }
然後,在config/routes.php
檔案中新增以下路由:
use AppControllerExcelController; Router::post('/excel/import', [ExcelController::class, 'import']);
- 建立視圖
在resources/views
目錄中建立一個import.blade.php
文件,加入以下表單程式碼:
<form action="/excel/import" method="post" enctype="multipart/form-data"> <input type="file" name="excel_file"> <button type="submit">导入</button> </form>
- 處理導入邏輯
在控制器的import
方法中,我們使用PhpSpreadsheet
# 函式庫加載上傳的Excel 文件,並將其轉換為陣列格式。然後,我們可以根據實際需求對資料進行處理。
匯出
匯出Excel檔案主要涉及兩個步驟:建立Excel檔案和下載Excel檔案。
- 建立Excel檔案
在控制器中,我們可以使用PhpSpreadsheet
函式庫建立一個 Excel 檔案。
use PhpOfficePhpSpreadsheetSpreadsheet; use PhpOfficePhpSpreadsheetWriterXlsx; public function export() { $spreadsheet = new Spreadsheet(); $worksheet = $spreadsheet->getActiveSheet(); // 设置表头 $worksheet->setCellValue('A1', '姓名') ->setCellValue('B1', '年龄') ->setCellValue('C1', '性别'); // 设置数据行 $data = [ ['张三', '20', '男'], ['李四', '30', '女'], ]; $row = 2; foreach ($data as $item) { $column = 'A'; foreach ($item as $value) { $worksheet->setCellValue($column . $row, $value); $column++; } $row++; } // 保存文件 $writer = new Xlsx($spreadsheet); $writer->save('storage/export.xlsx'); }
- 下載Excel檔案
匯出完成後,我們可以透過設定回應頭資訊來實現檔案下載的功能。
use PsrHttpMessageStreamInterface; use SymfonyComponentConsoleOutputStreamOutput; public function download() { $file = 'storage/export.xlsx'; return $this->response->withHeader('Content-Disposition', 'attachment;filename=' . $file) ->withHeader('Pragma', 'public') ->withHeader('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet') ->withHeader('Content-Length', filesize($file)) ->withBody(new StreamOutput(fopen($file, 'r'))); }
在路由檔案中新增以下路由:
Router::get('/excel/download', [ExcelController::class, 'download']);
將匯出的檔案儲存在storage
目錄下,並透過瀏覽器存取/ excel/download
即可實現檔案下載。
總結
本文詳細介紹如何使用Hyperf框架實現Excel檔案的匯入和匯出功能,並給出了具體的程式碼範例。透過簡單的配置和編碼,我們可以在Hyperf框架中快速實現Excel導入導出,提高開發效率。
以上是如何使用Hyperf框架進行Excel導入導出的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本文概述了為Swoole項目做出貢獻的方法,包括報告錯誤,提交功能,編碼和改進文檔。它討論了初學者開始貢獻的必要技能和步驟,以及如何找到緊迫的是

本文討論了在PHP中使用Swoole的異步I/O功能用於高性能應用程序。它涵蓋安裝,服務器設置和優化策略。單詞計數:159

Swoole的反應堆模型使用事件驅動的,非阻滯I/O架構來有效地管理高持續性場景,通過各種技術優化性能。(159個字符)(159個字符)

摘要:本文討論了通過識別,隔離和固定解決SWOORE應用程序中的內存洩漏,並強調了常見原因,例如不當資源管理和不受管理的Coroutines。 Swoole Tracker和Valgrind等工具


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

MantisBT
Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境