search
HomePHP FrameworkSwooleHow to use Hyperf framework for Excel import

How to use Hyperf framework for Excel import

Oct 21, 2023 am 08:42 AM
Instructionsexcel importhyperf framework

How to use Hyperf framework for Excel import

How to use the Hyperf framework to import Excel, you need specific code examples

Introduction:
With the development of informatization, spreadsheets play an important role in our daily work play an important role. During the development process, we often encounter situations where we need to import data from Excel into the system. This article will introduce how to use the Hyperf framework for Excel import and provide specific code examples.

1. Install the necessary plug-ins
Before using the Hyperf framework to import Excel, we need to install two plug-ins, namely PhpSpreadsheet and hyperf/excel. The former is a powerful PHP spreadsheet operation library, and the latter is an Excel extension of the Hyperf framework. Install through Composer:

composer require phpoffice/phpspreadsheet
composer require hyperf/excel

2. Create Excel import service

  1. Create Imports in the app/Excel directory directory, and create a new class in the Imports directory, named ExcelImport. This class inherits from IlluminateDatabaseEloquentCollection and will be used to store imported data.

    <?php
    
    namespace AppExcelImports;
    
    use IlluminateSupportCollection;
    
    class ExcelImport extends Collection
    {
     public function headingRow(): int
     {
         return 1;
     }
    }
  2. Create the Services directory under the app/Excel directory, and create a new one under the Services directory Class, named ExcelService. This class defines a import method to implement Excel import logic.

    <?php
    
    namespace AppExcelServices;
    
    use AppExcelImportsExcelImport;
    use Exception;
    use HyperfDiAnnotationInject;
    use PhpOfficePhpSpreadsheetIOFactory;
    use HyperfExcelEventsAfterWriting;
    use HyperfExcelWriter;
    
    class ExcelService
    {
     /**
      * @Inject
      * @var Writer
      */
     protected $writer;
    
     public function import($file): array
     {
         $import = new ExcelImport();
         
         try {
             $spreadsheet = IOFactory::load($file);
             $worksheet = $spreadsheet->getActiveSheet();
             
             $import = $worksheet->toArray();
    
             // 将数据导入到ExcelImport
             $import->push($import);
         } catch (Exception $exception) {
             throw new Exception("Error in importing excel: " . $exception->getMessage());
         }
         
         // 触发事件,将导入的数据派发出去供其他业务逻辑使用
         event(new AfterWriting($this->writer, $import));
    
         return $import->all();
     }
    }

3. Use Excel Import Service
Where we need to use Excel to import, we can use the just created ExcelService through dependency injection. The specific code is as follows:

<?php

namespace AppController;

use AppExcelServicesExcelService;
use PsrContainerContainerInterface;

class ExcelController extends AbstractController
{
    /**
     * @var ExcelService
     */
    protected $excelService;

    public function __construct(ContainerInterface $container, ExcelService $excelService)
    {
        parent::__construct($container);
        $this->excelService = $excelService;
    }

    public function import()
    {
        $file = $this->request->file('file');
        $filePath = $file->getPathname();

        $data = $this->excelService->import($filePath);

        // 对导入数据进行处理,插入数据库或者其他业务逻辑

        return $this->response->success($data); // 返回导入的数据
    }
}

4. Configure routing
Finally, configure routing in the config/routes.php file to handle Excel import requests:

<?php

use HyperfHttpServerRouterRouter;

Router::post('/excel/import', 'AppControllerExcelController@import');

Summary:
This article introduces how to use the Hyperf framework for Excel import and provides specific code examples. By creating an import service, calling service methods and configuring routes, we can easily implement the function of importing Excel data into the system. At the same time, we can also process the imported data according to specific needs to meet the requirements of business logic. Using this method, development efficiency can be greatly improved and the development process simplified.

The above is the detailed content of How to use Hyperf framework for Excel import. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

mPDF

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),

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools