How to use the Hyperf framework to import and export Excel
Abstract: This article will introduce how to implement the import and export functions of Excel files in the Hyperf framework, and give specific details. code example.
Keywords: Hyperf framework, Excel import, Excel export, code examples
Import
First, we need to ensure that the phpoffice/phpspreadsheet
library is installed in the project . It can be installed by executing the following command in the terminal:
composer require phpoffice/phpspreadsheet
- Creating Controllers and Routes
First, we need to create a controller to handle the imported logic. Create anExcelController.php
file in theapp/Controller
directory and add the following code:
<?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(); // 处理导入逻辑 } }
Then, in config/routes.php
Add the following route to the file:
use AppControllerExcelController; Router::post('/excel/import', [ExcelController::class, 'import']);
- Create a view
Create aimport.blade.php
file in theresources/views
directory , add the following form code:
<form action="/excel/import" method="post" enctype="multipart/form-data"> <input type="file" name="excel_file"> <button type="submit">导入</button> </form>
- Processing import logic
In theimport
method of the controller, we use thePhpSpreadsheet
library to load Upload the Excel file and convert it to array format. Then, we can process the data according to actual needs.
Export
Exporting an Excel file mainly involves two steps: creating the Excel file and downloading the Excel file.
- Create Excel file
In the controller, we can create an Excel file using thePhpSpreadsheet
library.
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'); }
- Download Excel file
After the export is completed, we can implement the file download function by setting the response header information.
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'))); }
Add the following route in the routing file:
Router::get('/excel/download', [ExcelController::class, 'download']);
Store the exported file in the storage
directory and access it through the browser/ Excel/download
can be used to download files.
Summary
This article introduces in detail how to use the Hyperf framework to implement the import and export functions of Excel files, and gives specific code examples. Through simple configuration and coding, we can quickly implement Excel import and export in the Hyperf framework to improve development efficiency.
The above is the detailed content of How to use Hyperf framework for Excel import and export. For more information, please follow other related articles on the PHP Chinese website!

本篇文章给大家带来了关于excel的相关知识,其中主要介绍了关于折叠表格的相关问题,就是分类汇总的功能,这样查看数据会非常的方便,下面一起来看一下,希望对大家有帮助。

在之前的文章《实用Excel技巧分享:利用 数据透视表 来汇总业绩》中,我们学习了下Excel数据透视表,了解了利用数据透视表来汇总业绩的方法。而今天我们来聊聊怎么计算时间差(年数差、月数差、周数差),希望对大家有所帮助!

本篇文章给大家带来了关于excel的相关知识,其中主要介绍了关于AGGREGATE函数的相关内容,该函数用法与SUBTOTAL函数类似,但在功能上比SUBTOTAL函数更加强大,下面一起来看一下,希望对大家有帮助。

在之前的文章《实用Word技巧分享:聊聊你没用过的“行号”功能》中,我们了解了Word中你肯定没用过的"行号”功能。今天继续实用Word技巧分享,看看Excel表格怎么借用Word进行分栏打印,快来收藏使用吧!

本篇文章给大家带来了关于excel的相关知识,其中主要介绍了关于zenmm制作倒计时牌的相关内容,使用Excel中的日期函数结合按指定时间刷新的VBA代码,即可制作出倒计时牌,下面一起来看一下,希望对大家有帮助。

在之前的文章《实用Excel技巧分享:原来“定位功能”这么有用!》中,我们了解了定位功能的妙用。而今天我们聊聊合并后的单元格如何实现筛选功能,分享一种复制粘贴和方法解决这个问题,另外还会给大家分享一种合并单元格的不错的替代方式。

本篇文章给大家带来了关于excel的相关知识,其中主要介绍了关于如何使用函数寻找总和为某个值的组合的问题,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Excel的相关知识,其中主要介绍了关于XLOOKUP函数的相关知识,包括了常规查询、逆向查询、返回多列、自动除错以及近似查找等内容,下面一起来看一下,希望对大家有帮助。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Mac version
God-level code editing software (SublimeText3)

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.