Home > PHP Framework > Swoole > How to use Hyperf framework for Excel import

How to use Hyperf framework for Excel import

王林
Release: 2023-10-21 08:42:55
Original
1482 people have browsed it

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
Copy after login

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;
     }
    }
    Copy after login
  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();
     }
    }
    Copy after login

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); // 返回导入的数据
    }
}
Copy after login

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');
Copy after login

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!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template