AI prompts
base on 🦉 Fast Excel import/export for Laravel <p align="center">
<img src="https://user-images.githubusercontent.com/36028424/40173202-9a03d68a-5a03-11e8-9968-6b7e3b4f8a1b.png">
</p>
[![Version](https://poser.pugx.org/rap2hpoutre/fast-excel/version?format=flat)](https://packagist.org/packages/rap2hpoutre/fast-excel)
[![License](https://poser.pugx.org/rap2hpoutre/fast-excel/license?format=flat)](https://packagist.org/packages/rap2hpoutre/fast-excel)
[![StyleCI](https://github.styleci.io/repos/128174809/shield?branch=master)](https://github.styleci.io/repos/128174809?branch=master)
[![Tests](https://github.com/rap2hpoutre/fast-excel/actions/workflows/tests.yml/badge.svg)](https://github.com/rap2hpoutre/fast-excel/actions/workflows/tests.yml)
[![Total Downloads](https://poser.pugx.org/rap2hpoutre/fast-excel/downloads)](https://packagist.org/packages/rap2hpoutre/fast-excel)
Fast Excel import/export for Laravel, thanks to [Spout](https://github.com/box/spout).
See [benchmarks](#benchmarks) below.
## Quick start
Install via composer:
```
composer require rap2hpoutre/fast-excel
```
Export a Model to `.xlsx` file:
```php
use Rap2hpoutre\FastExcel\FastExcel;
use App\User;
// Load users
$users = User::all();
// Export all users
(new FastExcel($users))->export('file.xlsx');
```
## Export
Export a Model or a **Collection**:
```php
$list = collect([
[ 'id' => 1, 'name' => 'Jane' ],
[ 'id' => 2, 'name' => 'John' ],
]);
(new FastExcel($list))->export('file.xlsx');
```
Export `xlsx`, `ods` and `csv`:
```php
$invoices = App\Invoice::orderBy('created_at', 'DESC')->get();
(new FastExcel($invoices))->export('invoices.csv');
```
Export only some attributes specifying columns names:
```php
(new FastExcel(User::all()))->export('users.csv', function ($user) {
return [
'Email' => $user->email,
'First Name' => $user->firstname,
'Last Name' => strtoupper($user->lastname),
];
});
```
Download (from a controller method):
```php
return (new FastExcel(User::all()))->download('file.xlsx');
```
## Import
`import` returns a Collection:
```php
$collection = (new FastExcel)->import('file.xlsx');
```
Import a `csv` with specific delimiter, enclosure characters and "gbk" encoding:
```php
$collection = (new FastExcel)->configureCsv(';', '#', 'gbk')->import('file.csv');
```
Import and insert to database:
```php
$users = (new FastExcel)->import('file.xlsx', function ($line) {
return User::create([
'name' => $line['Name'],
'email' => $line['Email']
]);
});
```
## Facades
You may use FastExcel with the optional Facade. Add the following line to ``config/app.php`` under the ``aliases`` key.
````php
'FastExcel' => Rap2hpoutre\FastExcel\Facades\FastExcel::class,
````
Using the Facade, you will not have access to the constructor. You may set your export data using the ``data`` method.
````php
$list = collect([
[ 'id' => 1, 'name' => 'Jane' ],
[ 'id' => 2, 'name' => 'John' ],
]);
FastExcel::data($list)->export('file.xlsx');
````
## Global helper
FastExcel provides a convenient global helper to quickly instantiate the FastExcel class anywhere in a Laravel application.
```php
$collection = fastexcel()->import('file.xlsx');
fastexcel($collection)->export('file.xlsx');
```
## Advanced usage
### Export multiple sheets
Export multiple sheets by creating a `SheetCollection`:
```php
$sheets = new SheetCollection([
User::all(),
Project::all()
]);
(new FastExcel($sheets))->export('file.xlsx');
```
Use index to specify sheet name:
```php
$sheets = new SheetCollection([
'Users' => User::all(),
'Second sheet' => Project::all()
]);
```
### Import multiple sheets
Import multiple sheets by using `importSheets`:
```php
$sheets = (new FastExcel)->importSheets('file.xlsx');
```
You can also import a specific sheet by its number:
```php
$users = (new FastExcel)->sheet(3)->import('file.xlsx');
```
Import multiple sheets with sheets names:
```php
$sheets = (new FastExcel)->withSheetsNames()->importSheets('file.xlsx');
```
### Export large collections with chunk
Export rows one by one to avoid `memory_limit` issues [using `yield`](https://www.php.net/manual/en/language.generators.syntax.php):
```php
function usersGenerator() {
foreach (User::cursor() as $user) {
yield $user;
}
}
// Export consumes only a few MB, even with 10M+ rows.
(new FastExcel(usersGenerator()))->export('test.xlsx');
```
### Add header and rows style
Add header and rows style with `headerStyle` and `rowsStyle` methods.
```php
use OpenSpout\Common\Entity\Style\Style;
$header_style = (new Style())->setFontBold();
$rows_style = (new Style())
->setFontSize(15)
->setShouldWrapText()
->setBackgroundColor("EDEDED");
return (new FastExcel($list))
->headerStyle($header_style)
->rowsStyle($rows_style)
->download('file.xlsx');
```
## Why?
FastExcel is intended at being Laravel-flavoured [Spout](https://github.com/box/spout):
a simple, but elegant wrapper around [Spout](https://github.com/box/spout) with the goal
of simplifying **imports and exports**. It could be considered as a faster (and memory friendly) alternative
to [Laravel Excel](https://laravel-excel.com/), with less features.
Use it only for simple tasks.
## Benchmarks
> Tested on a MacBook Pro 2015 2,7 GHz Intel Core i5 16 Go 1867 MHz DDR3.
Testing a XLSX export for 10000 lines, 20 columns with random data, 10 iterations, 2018-04-05. **Don't trust benchmarks.**
| | Average memory peak usage | Execution time |
|---|---|---|
| Laravel Excel | 123.56 M | 11.56 s |
| FastExcel | 2.09 M | 2.76 s |
Still, remember that [Laravel Excel](https://laravel-excel.com/) **has many more features.**
", Assign "at most 3 tags" to the expected json: {"id":"8080","tags":[]} "only from the tags list I provide: [{"id":77,"name":"3d"},{"id":89,"name":"agent"},{"id":17,"name":"ai"},{"id":54,"name":"algorithm"},{"id":24,"name":"api"},{"id":44,"name":"authentication"},{"id":3,"name":"aws"},{"id":27,"name":"backend"},{"id":60,"name":"benchmark"},{"id":72,"name":"best-practices"},{"id":39,"name":"bitcoin"},{"id":37,"name":"blockchain"},{"id":1,"name":"blog"},{"id":45,"name":"bundler"},{"id":58,"name":"cache"},{"id":21,"name":"chat"},{"id":49,"name":"cicd"},{"id":4,"name":"cli"},{"id":64,"name":"cloud-native"},{"id":48,"name":"cms"},{"id":61,"name":"compiler"},{"id":68,"name":"containerization"},{"id":92,"name":"crm"},{"id":34,"name":"data"},{"id":47,"name":"database"},{"id":8,"name":"declarative-gui "},{"id":9,"name":"deploy-tool"},{"id":53,"name":"desktop-app"},{"id":6,"name":"dev-exp-lib"},{"id":59,"name":"dev-tool"},{"id":13,"name":"ecommerce"},{"id":26,"name":"editor"},{"id":66,"name":"emulator"},{"id":62,"name":"filesystem"},{"id":80,"name":"finance"},{"id":15,"name":"firmware"},{"id":73,"name":"for-fun"},{"id":2,"name":"framework"},{"id":11,"name":"frontend"},{"id":22,"name":"game"},{"id":81,"name":"game-engine "},{"id":23,"name":"graphql"},{"id":84,"name":"gui"},{"id":91,"name":"http"},{"id":5,"name":"http-client"},{"id":51,"name":"iac"},{"id":30,"name":"ide"},{"id":78,"name":"iot"},{"id":40,"name":"json"},{"id":83,"name":"julian"},{"id":38,"name":"k8s"},{"id":31,"name":"language"},{"id":10,"name":"learning-resource"},{"id":33,"name":"lib"},{"id":41,"name":"linter"},{"id":28,"name":"lms"},{"id":16,"name":"logging"},{"id":76,"name":"low-code"},{"id":90,"name":"message-queue"},{"id":42,"name":"mobile-app"},{"id":18,"name":"monitoring"},{"id":36,"name":"networking"},{"id":7,"name":"node-version"},{"id":55,"name":"nosql"},{"id":57,"name":"observability"},{"id":46,"name":"orm"},{"id":52,"name":"os"},{"id":14,"name":"parser"},{"id":74,"name":"react"},{"id":82,"name":"real-time"},{"id":56,"name":"robot"},{"id":65,"name":"runtime"},{"id":32,"name":"sdk"},{"id":71,"name":"search"},{"id":63,"name":"secrets"},{"id":25,"name":"security"},{"id":85,"name":"server"},{"id":86,"name":"serverless"},{"id":70,"name":"storage"},{"id":75,"name":"system-design"},{"id":79,"name":"terminal"},{"id":29,"name":"testing"},{"id":12,"name":"ui"},{"id":50,"name":"ux"},{"id":88,"name":"video"},{"id":20,"name":"web-app"},{"id":35,"name":"web-server"},{"id":43,"name":"webassembly"},{"id":69,"name":"workflow"},{"id":87,"name":"yaml"}]" returns me the "expected json"