Skip to content

Commit

Permalink
Multiple improvements and file README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
aVadim483 committed Jun 12, 2023
1 parent fab5ce8 commit 764bd4c
Show file tree
Hide file tree
Showing 7 changed files with 462 additions and 16 deletions.
134 changes: 132 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,132 @@
# fast-excel-laravel
Lightweight and very fast XLSX Excel Spreadsheet Writer for Laravel
# FastExcelLaravel
Lightweight and very fast XLSX Excel Spreadsheet Writer for Laravel
(wrapper for [FastExcelWriter](https://packagist.org/packages/avadim/fast-excel-writer))

## Introduction

This library is a wrapper for avadim/fast-excel-writer, so it's also lightweight, fast, and requires a minimum of memory.
Using this library, you can export arrays, collections and models to a XLSX-file from your Laravel application.

## Installation

Install via composer:

```
composer require avadim/fast-excel-laravel
```
And then you can use facade ```Excel```

```php
// Create workbook
$excel = \Excel::create();
// do something...
// and save XLSX-file to default storage
$excel->saveTo('path/file.xlsx');

// or save file to specified disk
$excel->store('disk', 'path/file.xlsx');
```
Export a Model
```php

// Create workbook with sheet named 'Users'
$excel = \Excel::create('Users');

// Write all users to Excel file
$sheet->writeModel(Users::class);

// Write users with field names in the first row
$sheet->withHeaders()
->applyFontStyleBold()
->applyBorder('thin')
->writeModel(Users::class);

$excel->saveTo('path/file.xlsx');
```

Export any collections and arrays
```php
// Create workbook with sheet named 'Users'
$excel = \Excel::create('Users');

$sheet = $excel->getSheet();
$users = User::all();
$sheet->writeData($users);

$sheet = $excel->makeSheet('Records');
$records = \DB::table('users')->where('age', '>=', 21)->get(['id', 'name', 'birthday']);
$sheet->writeData($records);

$sheet = $excel->makeSheet('Collection');
$collection = collect([
[ 'id' => 1, 'site' => 'google.com' ],
[ 'id' => 2, 'site.com' => 'youtube.com' ],
]);
$sheet->writeData($collection);

$sheet = $excel->makeSheet('Array');
$array = [
[ 'id' => 1, 'name' => 'Helen' ],
[ 'id' => 2, 'name' => 'Peter' ],
];
$sheet->writeData($array);

$sheet = $excel->makeSheet('Callback');
$sheet->writeData(function () {
foreach (User::cursor() as $user) {
yield $user;
}
});

```

## Advanced usage

See detailed documentation for avadim/fast-excel-laravel here: https://github.com/aVadim483/fast-excel-writer/tree/master#readme

```php
$excel = \Excel::create('Users');
$sheet = $excel->getSheet();

$sheet->setColWidth('B', 12);
$sheet->setColOptions('c', ['width' => 12, 'text-align' => 'center']);
$sheet->setColWidth('d', 'auto');

$title = 'This is demo of avadim/fast-excel-laravel';
// begin area for direct access to cells
$area = $sheet->beginArea();
$area->setValue('A2:D2', $title)
->applyFontSize(14)
->applyFontStyleBold()
->applyTextCenter();

$area
->setValue('a4:a5', '#')
->setValue('b4:b5', 'Number')
->setValue('c4:d4', 'Movie Character')
->setValue('c5', 'Birthday')
->setValue('d5', 'Name')
;

$area->withRange('a4:d5')
->applyBgColor('#ccc')
->applyFontStyleBold()
->applyOuterBorder('thin')
->applyInnerBorder('thick')
->applyTextCenter();

$sheet->writeAreas();

$sheet->writeData($data);
$excel->saveTo($testFileName);

```

## Do you want to support FastExcelLaravel?

if you find this package useful you can support and donate to me for a cup of coffee:

* USDT (TRC20) TSsUFvJehQBJCKeYgNNR1cpswY6JZnbZK7
* USDT (ERC20) 0x5244519D65035aF868a010C2f68a086F473FC82b

Or just give me star on GitHub :)
16 changes: 9 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,23 @@
],
"type": "library",
"homepage": "https://github.com/aVadim483/fast-excel-laravel",
"license": "MIT",
"autoload": {
"psr-4": {
"avadim\\FastExcelLaravel\\": "./src/FastExcelLaravel"
}
},
"require": {
"php": "^7.4|^8.1",
"ext-json": "*",
"avadim/fast-excel-writer": "^4.0",
"illuminate/support": "^6.0|^7.0|^8.0|^9.0|^10.0",
"illuminate/contracts": "^6.0|^7.0|^8.0|^9.0|^10.0"
},
"require-dev": {
"orchestra/testbench": "^4.0|^5.0|^6.0",
"phpunit/phpunit": "^8.0|^9.0"
},
"license": "MIT",
"autoload": {
"psr-4": {
"avadim\\FastExcelLaravel\\": "./src/FastExcelLaravel"
}
"phpunit/phpunit": "^8.0|^9.0",
"avadim/fast-excel-reader": "^2.2"
},
"extra": {
"laravel": {
Expand Down
4 changes: 2 additions & 2 deletions src/FastExcelLaravel/Excel.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ public function __construct(?array $options = [])

}

public static function create($sheets = null, ?array $options = [])
public static function create($sheets = null, ?array $options = []): ExcelWriter
{
return ExcelWriter::create($sheets);
return ExcelWriter::create($sheets, $options);
}
}
13 changes: 9 additions & 4 deletions src/FastExcelLaravel/ExcelWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,18 @@ public static function create($sheets = null, ?array $options = []): ExcelWriter
}
}
$excel = new self($options);
if (is_array($sheets)) {
foreach ($sheets as $sheetName) {
$excel->makeSheet($sheetName);
if ($sheets) {
if (is_array($sheets)) {
foreach ($sheets as $sheetName) {
$excel->makeSheet($sheetName);
}
}
else {
$excel->makeSheet((string)$sheets);
}
}
else {
$excel->makeSheet($sheets);
$excel->makeSheet();
}

return $excel;
Expand Down
3 changes: 2 additions & 1 deletion src/FastExcelLaravel/SheetWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ protected function _writeHeader($record)

public function writeRow(array $rowValues = [], array $rowStyle = null, array $cellStyles = null): Sheet
{
if ($this->dataRowCount > 0 && $this->headers['header_keys']) {
if ($this->dataRowCount > 0 && !empty($this->headers['header_keys'])) {
$rowData = [];
foreach ($this->headers['header_keys'] as $key) {
if (isset($rowValues[$key])) {
Expand Down Expand Up @@ -131,6 +131,7 @@ public function withHeaders(?array $headers = [], ?array $rowStyle = [], ?array
'rowStyle' => $rowStyle,
'colStyles' => $colStyles,
];
$this->lastTouch['ref'] = 'row';

return $this;
}
Expand Down
Loading

0 comments on commit 764bd4c

Please sign in to comment.