PHPackages                             odan/excel - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [PDF &amp; Document Generation](/categories/documents)
4. /
5. odan/excel

ActiveLibrary[PDF &amp; Document Generation](/categories/documents)

odan/excel
==========

In-memory Excel file writer

0.2.0(6mo ago)14MITPHPPHP ^8.1CI passing

Since Nov 11Pushed 6mo ago1 watchersCompare

[ Source](https://github.com/odan/excel)[ Packagist](https://packagist.org/packages/odan/excel)[ Docs](https://github.com/odan/excel)[ RSS](/packages/odan-excel/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (2)Dependencies (5)Versions (3)Used By (0)

odan/excel
==========

[](#odanexcel)

[![Latest Version on Packagist](https://camo.githubusercontent.com/f4e95821c50b87508f5b77c664ffd4eda15c84aa916a17013a5aee8bdfae0fc8/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f72656c656173652f6f64616e2f657863656c2e737667)](https://github.com/odan/excel/releases)[![Software License](https://camo.githubusercontent.com/074b89bca64d3edc93a1db6c7e3b1636b874540ba91d66367c0e5e354c56d0ea/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e737667)](LICENSE)[![Build Status](https://github.com/odan/excel/workflows/build/badge.svg)](https://github.com/odan/excel/actions)[![Total Downloads](https://camo.githubusercontent.com/66dd7adf932e17dc0f15737bf9c06ca6d350709e89e5249f3f258b6f0e3a443f/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6f64616e2f657863656c2e737667)](https://packagist.org/packages/odan/excel/stats)

Extreme fast in-memory Excel (XLSX) file writer.

Requirements
------------

[](#requirements)

- PHP 8.1 - 8.5

Features
--------

[](#features)

- Optimized for minimal memory usage and high performance.
- Compatibility with Microsoft Excel 2007-365 (ISO/IEC 29500-1:2016).
- Compatibility with LibreOffice / OpenOffice Calc.
- In-memory operation by default.
- Optional hard disk access, when memory limitations are reached.
- Multiple sheets in a workbook.
- Header columns with bold font.
- Custom worksheet name.
- Data types for rows: string, int, float
- Data types for columns: string

Limitations
-----------

[](#limitations)

The purpose of this package is to provide a very fast and memory efficient Excel (XLSX) file generator. It is designed for very fast data output, but not for fancy worksheet styles. If you need more layout and color options, you may better use a different package, such as PhpSpreadsheet.

- Number of workbooks and sheets: Limited by available memory and system resources.
- Maximal number of columns: 16.384 (specification limit)
- Maximal number of rows: 1.048.576 (specification limit)
- Font styles: 2 (normal for rows and **bold** for columns)

Installation
------------

[](#installation)

```
composer require odan/excel
```

Usage
-----

[](#usage)

```
use Odan\Excel\ExcelWorkbook;
use Odan\Excel\ZipDeflateStream;

$workbook = new ExcelWorkbook();
$sheet = $workbook->addSheet('My Sheet');

// Write header columns
$columns = ['Date', 'Name', 'Amount'];
$sheet->addColumns($columns);

// Write data
$rows = [
    ['2023-01-31', 'James', 220],
    ['2023-03-28', 'Mike', 153.5],
    ['2024-07-02', 'Sally', 34.12],
];

foreach ($rows as $row) {
    $sheet->addRow($row);
}

// Save as Excel file in memory
$file = new ZipDeflateStream();
$workbook->save($file);
```

**Generating only In-Memory Excel file**

This data is a pure in-memory stream `php://memory` (default) that never overflows onto the hard disk, regardless of the amount of written data.

```
use Odan\Excel\ZipDeflateStream;

// ...

$file = new ZipDeflateStream();
$workbook->save($file);
```

**Generating temporary files**

The `php://temp` stream is designed for temporary data storage in memory.

However, if the amount of data written exceeds a certain threshold (usually around 2KB or 8KB, depending on PHP versions and configurations), PHP may automatically switch to using temporary files on disk to store the data. This is done to conserve memory when dealing with large amounts of data.

This kind of stream is suitable for most scenarios where you need temporary in-memory storage, but it should automatically switch to using temporary files on disk to store the excess data when it overflows a certain threshold.

```
use Odan\Excel\ZipDeflateStream;

// ...

$file = new ZipDeflateStream('php://temp');
$workbook->save($file);
```

The memory limit of `php://temp` can be controlled by appending `/maxmemory:NN`, where NN is the maximum amount of data to keep in memory before using a temporary file, in bytes.

This optional parameter allows setting the memory limit before `php://temp` starts using a temporary file.

```
use Odan\Excel\ZipDeflateStream;

// ...

// Set the limit to 5 MB.
$maxMb = 5 * 1024 * 1024;
$file = new ZipDeflateStream('php://temp/maxmemory:' . $maxMb);
$workbook->save($file);
```

**Save file in filesystem**

If the file does not exist, it will be created. If it already exists, its content will be truncated (cleared) when you write data to it. Make sure the server has write permissions.

Directly as file stream...

```
use Odan\Excel\ZipDeflateStream;

// ...

$file = new ZipDeflateStream('example.xlsx');
$workbook->save($file);
```

... or with stream\_get\_contents.

```
use Odan\Excel\ZipDeflateStream;

// ...

$file = new ZipDeflateStream();
$workbook->save($file);

$data = stream_get_contents($file->getStream());
file_put_contents('filename.xlsx', $data);
```

**Generating Excel file on hard disk with write permissions**

```
use Odan\Excel\ZipDeflateStream;

// ...

$filename = 'example.xlsx';

// Create an empty file using touch
touch($filename);

// Set write permissions to the file
chmod($filename, 0644);

$file = new ZipDeflateStream($filename);
$workbook->save($file);
```

**Reading the stream contents as string**

```
use Odan\Excel\ZipDeflateStream;

// ...

$file = new ZipDeflateStream();
$workbook->save($file);

// Read contents of stream into a string
$data = stream_get_contents($file->getStream());
```

**Stream directly to the HTTP response**

To send an existing stream directly to the HTTP response, you can use the `fpassthru` function. This function reads from an open file pointer and sends the contents directly to the output buffer.

Here's an example of how to do this:

```
