PHP code example of fersot / excel-to

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

    

fersot / excel-to example snippets


use Fersot\ExcelTo\ExcelTo;

// Static shortcuts (backwards compatible)
$array      = ExcelTo::array('path/to/file.xlsx');
$collection = ExcelTo::collection('path/to/file.xlsx');
$json       = ExcelTo::json('path/to/file.xlsx');

// Fluent API (recommended)
$data = ExcelTo::file('path/to/file.xlsx')
    ->sheet('Clientes')
    ->skipEmpty()
    ->only(['nombre', 'email', 'ciudad'])
    ->dateFormat('Y-m-d')
    ->toArray();

ExcelTo::file('path/to/file.xlsx')->toArray();

ExcelTo::upload($request->file('excel'))->toCollection();

ExcelTo::file('path/to/file.csv')->toArray();

$instance = ExcelTo::file('file.xlsx');

$instance->toArray();       // PHP array
$instance->toCollection();  // Illuminate\Support\Collection
$instance->toJson();        // JSON string
$instance->toJson(JSON_PRETTY_PRINT); // JSON with flags

// By name
ExcelTo::file('file.xlsx')->sheet('Sheet2')->toArray();

// By zero-based index
ExcelTo::file('file.xlsx')->sheet(1)->toArray();

ExcelTo::file('file.xlsx')->skipEmpty()->toArray();

// Disable explicitly
ExcelTo::file('file.xlsx')->skipEmpty(false)->toArray();

ExcelTo::file('file.xlsx')->headerRow(2)->toArray();

// Default: 'd/m/Y'
ExcelTo::file('file.xlsx')->dateFormat('Y-m-d')->toArray();
ExcelTo::file('file.xlsx')->dateFormat('d-m-Y H:i')->toArray();

// Keep only these columns
ExcelTo::file('file.xlsx')->only(['nombre', 'email'])->toArray();

// Exclude these columns
ExcelTo::file('file.xlsx')->except(['id', 'internal_code'])->toArray();

use Fersot\ExcelTo\ExcelTo;

$data = [
    ['producto' => 'Widget', 'precio' => 9.99,  'stock' => 100],
    ['producto' => 'Gadget', 'precio' => 19.99, 'stock' => 50],
];

ExcelTo::fromArray($data)->export('storage/exports/report.xlsx');

// From a Collection
ExcelTo::fromCollection($collection)->export('storage/exports/report.xlsx');

return [
    'date_format' => 'd/m/Y',   // Default date output format
    'skip_empty'  => false,     // Skip empty rows by default
    'header_row'  => 1,         // Default header row number
];

use Fersot\ExcelTo\ExcelTo;

$report = ExcelTo::file(storage_path('imports/ventas_2024.xlsx'))
    ->sheet('Enero')
    ->headerRow(2)
    ->skipEmpty()
    ->only(['cliente', 'importe', 'fecha'])
    ->dateFormat('Y-m-d')
    ->toCollection();

$report->each(fn($row) => \Log::info($row));