odan/excel
Composer 安装命令:
composer require odan/excel
包简介
In-memory Excel file writer
README 文档
README
Extreme fast in-memory Excel (XLSX) file writer.
Requirements
- PHP 8.1 - 8.5
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
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
composer require odan/excel
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:
<?php // Set the content type to Excel header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); header('Content-Disposition: attachment; filename="example.xlsx"'); // ... $stream = $file->getStream(); // Send the stream contents directly to the HTTP response fpassthru($stream); // Close the stream fclose($stream);
Stream directly to the PSR-7 HTTP response
To stream a file directly to an PSR-7 HTTP response using the Nyholm PSR-7 package, you may use it as follows:
use Nyholm\Psr7\Response; use Nyholm\Psr7\Stream; use Odan\Excel\ZipDeflateStream; // ... $file = new ZipDeflateStream(); $workbook->save($file); // Generate safe filename $outputFilename = rawurlencode(basename('example.xlsx')); $contentDisposition = sprintf("attachment; filename*=UTF-8''%s", $outputFilename); // Add the response headers $response = $response ->withHeader('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet') ->withHeader('Content-Disposition', $contentDisposition) ->withHeader('Pragma', 'private') ->withHeader('Cache-Control', 'private, must-revalidate') ->withHeader('Content-Transfer-Encoding', 'binary'); // Set the response body to the file stream $response = $response->withBody(new Stream($file->getStream()));
Change the filename accordingly.
Using the ZipStream-PHP package
When working with very large Excel files, typically over 4 GB, you can use the ZipStream-PHP package to create Excel files in the ZIP64 format, which is designed for handling such large files.
Installation
composer require maennchen/zipstream-php
Next, use the Odan\Excel\Zip64Stream class for creating Excel
files that offer improved compatibility and support larger file sizes.
use Odan\Excel\ExcelWorkbook; use Odan\Excel\Zip64Stream; $workbook = new ExcelWorkbook(); $sheet = $workbook->addSheet('My Sheet'); // 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 $file = new Zip64Stream('filename.xlsx'); $workbook->save($file);
License
The MIT License (MIT). Please see License File for more information.
odan/excel 适用场景与选型建议
odan/excel 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4 次下载、GitHub Stars 达 1, 最近一次更新时间为 2023 年 11 月 10 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「excel」 「xlsx」 「memory」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 odan/excel 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 odan/excel 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 odan/excel 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
repository php library
This Symfony bundle integrates PhpSpreadsheet into Symfony using Twig.
PHP Library to read and write spreadsheet files (CSV, XLSX and ODS), in a fast and scalable way
PHPExcel - OpenXML - Read, Create and Write Spreadsheet documents in PHP - Spreadsheet engine
Yii2 export extension
PHP Excel Library
统计信息
- 总下载量: 4
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 6
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-11-10