xaamin/simple-excel
Composer 安装命令:
composer require xaamin/simple-excel
包简介
Excel reader and writer
README 文档
README
This package allows you to easily read and write simple Excel and CSV files. Behind the scenes generators are used to ensure low memory usage, even when working with large files.
Here's an example on how to read an Excel or CSV.
$reader = ExcelReader::create($pathToFile)
foreach ($reader->rows() as $row) {
// Process the row
}
If $pathToFile ends with .csv or .txt a CSV file is assumed. If it ends with .xlsx, an Excel file is assumed.
Support us
Installation
You can install the package via composer:
composer require xaamin/simple-excel
Usage
Reading a CSV
Imagine you have a CSV with this content.
email,first_name
john@example.com,john
jane@example.com,jane
$rows = ExcelReader::create($pathToCsv);
foreach ($reader->rows() as $row) {
// In the first pass $row will contain
// ['email' => 'john@example.com', 'first_name' => 'john']
});
foreach ($reader->chunk(2) as $chunk) {
// Each chunk contains 2 rows
});
Reading an Excel file
Reading an Excel file is identical to reading a CSV file. Just make sure that the path given to the create method of ExcelReader ends with xlsx.
Reading a file without titles
If the file you are reading does not contain a title row, then you should use the noHeaderRow() method.
// $rows is an instance of Illuminate\Support\LazyCollection
$rows = ExcelReader::create($pathToCsv)
->withHeader(false);
foreach ($reader->rows() as $row) {
// in the first pass $rowProperties will contain
// [0 => 'john@example', 1 => 'john']
}
Manually working with the reader object
Under the hood this package uses the box/spout package. You can get to the underlying reader that implements \Box\Spout\Reader\ReaderInterface by calling the getReader method.
$reader = ExcelReader::create($pathToCsv)->getReader();
Writing files
Here's how you can write a CSV file:
$writer = ExcelWriter::create($pathToCsv)
->addRow([
'first_name' => 'John',
'last_name' => 'Doe',
])
->addRow([
'first_name' => 'Jane',
'last_name' => 'Doe',
]);
The file at pathToCsv will contain:
first_name,last_name
John,Doe
Jane,Doe
Writing an Excel file
Writing an Excel file is identical to writing a csv. Just make sure that the path given to the create method of ExcelWriter ends with xlsx.
Streaming an Excel file to the browser
Instead of writing a file to disk, you can stream it directly to the browser.
$writer = ExcelWriter::downloadAs('your-export.xlsx')
->addRow([
'first_name' => 'John',
'last_name' => 'Doe',
])
->addRow([
'first_name' => 'Jane',
'last_name' => 'Doe',
])
->sendToBrowser();
Writing multiple rows at once
You can use addRows instead of addRow to add multiple rows at once.
$writer = ExcelWriter::downloadAs('your-export.xlsx')
->addRows([
[
'first_name' => 'John',
'last_name' => 'Doe',
],
[
'first_name' => 'Jane',
'last_name' => 'Doe',
],
])->sendToBrowser();
Writing a file without titles
If the file you are writing should not have a title row added automatically, then you should use the noHeaderRow() method.
$writer = ExcelWriter::create($pathToCsv)
->withHeader(false)
->addRow([
'first_name' => 'Jane',
'last_name' => 'Doe',
]);
});
This will output:
Jane,Doe
Adding layout
Under the hood this package uses the box/spout package. That package contains a StyleBuilder that you can use to format rows. Styles can only be used on excel documents.
use Box\Spout\Common\Entity\Style\Color;
use Box\Spout\Writer\Common\Creator\Style\StyleBuilder;
$style = (new StyleBuilder())
->setFontBold()
->setFontSize(15)
->setFontColor(Color::BLUE)
->setShouldWrapText()
->setBackgroundColor(Color::YELLOW)
->build();
$writer->addRow(['values, 'of', 'the', 'row'], $style)
For more information on styles head over to the Spout docs.
Using an alternative delimiter
By default the ExcelReader will assume that the delimiter is a ,.
This is how you can use an alternative delimiter:
ExcelWriter::create($pathToCsv)->useDelimiter(';');
Getting the number of rows written
You can get the number of rows that are written. This number includes the automatically added header row.
$writerWithAutomaticHeader = ExcelWriter::create($this->pathToCsv)
->addRow([
'first_name' => 'John',
'last_name' => 'Doe',
]);
$writerWithoutAutomaticHeader->getNumberOfRows() // returns 2
Manually working with the writer object
Under the hood this package uses the box/spout package. You can get to the underlying writer that implements \Box\Spout\Reader\WriterInterface by calling the getWriter method.
$writer = ExcelWriter::create($pathToCsv)->getWriter();
Testing
Coming soon
Contributing
Please see CONTRIBUTING for details.
Security
If you discover any security related issues, please email freek@spatie.be instead of using the issue tracker.
License
The MIT License (MIT). Please see License File for more information.
xaamin/simple-excel 适用场景与选型建议
xaamin/simple-excel 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.43k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2022 年 02 月 06 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「excel」 「xls」 「xlsx」 「csv」 「Simple」 「fast」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 xaamin/simple-excel 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 xaamin/simple-excel 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 xaamin/simple-excel 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
This Symfony bundle integrates PhpSpreadsheet into Symfony using Twig.
PHPExcel - OpenXML - Read, Create and Write Spreadsheet documents in PHP - Spreadsheet engine
PHPExcel - OpenXML - Read, Create and Write Spreadsheet documents in PHP - Spreadsheet engine
Yii2 export extension
PHP Excel Library
PHPExcel - OpenXML - Read, Create and Write Spreadsheet documents in PHP - Spreadsheet engine
统计信息
- 总下载量: 2.43k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 10
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-02-06