fersot/excel-to

Laravel package that converts Excel/CSV files to JSON, Collection, or Array — with fluent API, sheet selection, column filtering, and export support.

Maintainers

Package info

github.com/fersot/excel-to

pkg:composer/fersot/excel-to

Statistics

Installs: 26

Dependents: 1

Suggesters: 0

Stars: 0

Open Issues: 0

v2.0.0 2026-03-26 10:04 UTC

This package is auto-updated.

Last update: 2026-03-26 10:07:28 UTC


README

A Laravel package for converting Excel and CSV files to JSON, Laravel Collections, or PHP Arrays — with a fluent API, multi-sheet support, column filtering, date formatting, and export capabilities.

Latest Version PHP Version License Tests

Installation

composer require fersot/excel-to

Laravel auto-discovers the service provider and facade automatically.

Optionally publish the config file:

php artisan vendor:publish --tag=excel-to-config

Quick Start

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();

Reading Files

Load from path

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

Load from Laravel UploadedFile

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

Load a CSV

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

Output Formats

$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

Fluent Options

All options can be chained in any order.

Select a Sheet

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

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

With a single-sheet file the result is a flat array. With multiple sheets, each sheet is a key in the result.

Skip Empty Rows

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

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

Custom Header Row

Useful when the first row is a title and headers start on row 2:

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

Date Format

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

Filter Columns

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

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

Exporting to Excel

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');

Configuration

After publishing, edit config/excel-to.php:

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
];

Options set via the fluent API always override config values.

Full Example

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

Requirements

Dependency Version
PHP ^8.1
Laravel ^9.0 | ^10.0 | ^11.0 | ^12.0
PhpSpreadsheet ^1.29 | ^2.0

Testing

composer install
./vendor/bin/phpunit --testdox

25 tests covering all features.

Author

Hember Colmenareshemberfer@gmail.com · GitHub

Support Me ☕️

If you find this package useful, consider buying me a coffee.

Buy Me a Coffee

License

MIT — see LICENSE.