Laravel Cloud is here! Zero-config managed infrastructure for Laravel apps. Deploy now.

Simple Excel

spatie/simple-excel image

Simple Excel stats

Downloads
2.8M
Stars
990
Open Issues
0
Forks
110

View on GitHub →

Read and write simple Excel and CSV files

Spatie Simple Excel

The Spatie Simple Excel package provides a streamlined approach to reading and writing Excel and CSV files in Laravel, focusing on simplicity and minimal memory usage. Utilizing generators, it efficiently handles large files without substantial memory overhead, making it ideal for applications that require handling of extensive datasets.

Features

  • Simple API: Easily read and write Excel and CSV files with a straightforward, intuitive API.
  • Low Memory Usage: Uses generators to manage large files efficiently.
  • LazyCollections Integration: Works with Laravel's LazyCollections for further memory efficiency.
  • Flexible Reading Options: Supports reading files with or without headers and offers methods for custom headers and multiple sheets management.
  • Robust Writing Capabilities: Includes features like streaming files to the browser, adding multiple rows simultaneously, and extensive styling and formatting options.
  • Excel-Specific Features: Handles multiple sheets, custom styles, and formulas within cells.

Installation

Install the package via composer:

composer require spatie/simple-excel

Usage

Reading Files

You can read files by specifying the path, and the package automatically handles different file types based on the extension:

use Spatie\SimpleExcel\SimpleExcelReader;
 
$rows = SimpleExcelReader::create($pathToCsv)->getRows();
$rows->each(function(array $rowProperties) {
// process each row
});

Writing Files

Writing to a file is straightforward, with methods to add single or multiple rows:

use Spatie\SimpleExcel\SimpleExcelWriter;
 
$writer = SimpleExcelWriter::create($pathToCsv)
->addRow(['first_name' => 'John', 'last_name' => 'Doe'])
->addRow(['first_name' => 'Jane', 'last_name' => 'Doe']);

Advanced Features

  • Handling Multiple Sheets: Select sheets by name or index when reading and create new sheets when writing.
  • Cell Formatting: Apply styles and formatting to cells specifically for Excel files.
  • Streaming Downloads: Directly stream Excel files to the browser, ideal for exporting data on-the-fly.
  • Custom Delimiters and Headers: Customize how data is read and interpreted, including support for files without headers.

Testing

Run the tests with:

composer test

Contributing

Contributions are welcome, and the package is open-source on GitHub. It also operates as "postcardware," where users are encouraged to send a postcard if they use the software in their production environment.

For a detailed guide on reading and writing capabilities, including working with large files and customizing headers, rows, and formatting, see the package's official documentation.

spatie photo

We create open source, digital products and courses for the developer community


Spatie Simple Excel Related Articles

How to process large CSV files with Laravel image

How to process large CSV files with Laravel

Read article
Laravel Deleted Models Package image

Laravel Deleted Models Package

Read article
Data Transfer Object V3 Modernizes DTOs With PHP 8 Features image

Data Transfer Object V3 Modernizes DTOs With PHP 8 Features

Read article
Spatie Laravel Flash Package image

Spatie Laravel Flash Package

Read article
Laravel View Models image

Laravel View Models

Read article
Laravel Event Projector Released image

Laravel Event Projector Released

Read article
Get expert guidance in a few days with a Laravel code review logo

Get expert guidance in a few days with a Laravel code review

Expert code review! Get clear, practical feedback from two Laravel devs with 10+ years of experience helping teams build better apps.

Get expert guidance in a few days with a Laravel code review
Harpoon: Next generation time tracking and invoicing logo

Harpoon: Next generation time tracking and invoicing

The next generation time-tracking and billing software that helps your agency plan and forecast a profitable future.

Harpoon: Next generation time tracking and invoicing
Tinkerwell logo

Tinkerwell

The must-have code runner for Laravel developers. Tinker with AI, autocompletion and instant feedback on local and production environments.

Tinkerwell
Laravel Cloud logo

Laravel Cloud

Easily create and manage your servers and deploy your Laravel applications in seconds.

Laravel Cloud
SaaSykit: Laravel SaaS Starter Kit logo

SaaSykit: Laravel SaaS Starter Kit

SaaSykit is a Multi-tenant Laravel SaaS Starter Kit that comes with all features required to run a modern SaaS. Payments, Beautiful Checkout, Admin Panel, User dashboard, Auth, Ready Components, Stats, Blog, Docs and more.

SaaSykit: Laravel SaaS Starter Kit
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum