1. Go to this page and download the library: Download openyam/hyperf-excel library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
openyam / hyperf-excel example snippets
use Hyperf\Collection\Collection;
use OpenYam\HyperfExcel\Concerns\FromCollection;
use OpenYam\HyperfExcel\Concerns\ShouldAutoSize;
use OpenYam\HyperfExcel\Concerns\WithHeadings;
use OpenYam\HyperfExcel\Concerns\WithMapping;
use OpenYam\HyperfExcel\Exportable;
final class UsersExport implements FromCollection, WithHeadings, WithMapping, ShouldAutoSize
{
use Exportable;
public function __construct(private readonly Collection $users) {}
public function collection(): Collection
{
return $this->users;
}
public function headings(): array
{
return ['ID', '姓名', '邮箱'];
}
public function map(mixed $user): array
{
return [$user->id, $user->name, $user->email];
}
}
// Controller 中返回 PSR-7 下载响应
return (new UsersExport($users))->download('users.xlsx');
// 保存到本地路径或 Hyperf Filesystem 磁盘
$export->store(BASE_PATH . '/runtime/users.xlsx');
$export->store('exports/users.xlsx', 's3');
use Hyperf\Database\Model\Model;
use OpenYam\HyperfExcel\Concerns\SkipsEmptyRows;
use OpenYam\HyperfExcel\Concerns\ToModel;
use OpenYam\HyperfExcel\Concerns\WithBatchInserts;
use OpenYam\HyperfExcel\Concerns\WithChunkReading;
use OpenYam\HyperfExcel\Concerns\WithHeadingRow;
use OpenYam\HyperfExcel\Concerns\WithValidation;
use OpenYam\HyperfExcel\Importable;
final class UsersImport implements ToModel, WithHeadingRow, WithValidation, WithBatchInserts, WithChunkReading, SkipsEmptyRows
{
use Importable;
public function headingRow(): int { return 1; }
public function batchSize(): int { return 500; }
public function chunkSize(): int { return 500; }
public function rules(): array
{
return ['email' => ['
use OpenYam\HyperfExcel\Concerns\Queueable;
use OpenYam\HyperfExcel\Concerns\ShouldQueue;
final class ReportExport implements FromQuery, ShouldQueue, Queueable
{
use Exportable;
private int $tenantId;
public function toQueuePayload(): array { return ['tenant_id' => $this->tenantId]; }
public function useQueuePayload(array $payload): void { $this->tenantId = (int) $payload['tenant_id']; }
// query() ...
}
$operation = $export->queue('exports/report.xlsx', 's3', options: ['queue' => 'default']);