Polyscope - The agent-first dev environment for Laravel

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

Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.


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
Lucky Media logo

Lucky Media

Get Lucky Now - the ideal choice for Laravel Development, with over a decade of experience!

Lucky Media
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
Shift logo

Shift

Running an old Laravel version? Instant, automated Laravel upgrades and code modernization to keep your applications fresh.

Shift
CodeKudu logo

CodeKudu

Stand-ups, Retrospectives, and 360° Feedback for the entire team. 50% off with code LARAVELNEWS.

CodeKudu
Securing Laravel logo

Securing Laravel

The essential security resource for Laravel devs, covering everything you need to keep your apps secure. Sign up to receive weekly security tips and monthly in depth articles, diving deep into security concepts you need to know!

Securing Laravel
Laravel Cloud logo

Laravel Cloud

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

Laravel Cloud