PHP code example of pittacusw / excel

1. Go to this page and download the library: Download pittacusw/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/ */

    

pittacusw / excel example snippets


use PittacusW\Excel\Excel;

class ExportUsersController
{
    public function __invoke(Excel $excel)
    {
        return $excel->download(
            rows: [
                ['name' => 'Ada', 'email' => '[email protected]'],
                ['name' => 'Grace', 'email' => '[email protected]'],
            ],
            fileName: 'users',
        );
    }
}

use PittacusW\Excel\Facades\Excel;

$rows = Excel::import(
    file: $request->file('users.xlsx'),
    headingRow: 1,
);

use PittacusW\Excel\Excel;

$excel->download(
    rows: [
        ['name' => 'Ada', 'email' => '[email protected]'],
    ],
    headings: ['Name', 'Email'],
    fileName: 'users',
);

$excel->store(
    rows: [
        ['name' => 'Ada', 'email' => '[email protected]'],
    ],
    filePath: 'exports/users.xlsx',
);

$contents = $excel->raw(
    rows: [
        ['name' => 'Ada', 'email' => '[email protected]'],
    ],
);

use PittacusW\Excel\Excel;

$rows = $excel->import(
    file: storage_path('app/imports/users.xlsx'),
    headingRow: 1,
);

download(iterable $rows, array $headings = [], ?string $fileName = null, ?string $writerType = null, array $headers = [])
store(iterable $rows, string $filePath, array $headings = [], ?string $disk = null, ?string $writerType = null, array|string $diskOptions = [])
raw(iterable $rows, array $headings = [], ?string $writerType = null)
import(string|\Illuminate\Http\UploadedFile $file, int $headingRow = 1, ?string $disk = null, ?string $readerType = null)
makeExport(iterable $rows, array $headings = [], ?string $fileName = null)
makeImport(int $headingRow = 1)