Skip to content

Commit 764bd4c

Browse files
committed
Multiple improvements and file README.md
1 parent fab5ce8 commit 764bd4c

File tree

7 files changed

+462
-16
lines changed

7 files changed

+462
-16
lines changed

README.md

Lines changed: 132 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,132 @@
1-
# fast-excel-laravel
2-
Lightweight and very fast XLSX Excel Spreadsheet Writer for Laravel
1+
# FastExcelLaravel
2+
Lightweight and very fast XLSX Excel Spreadsheet Writer for Laravel
3+
(wrapper for [FastExcelWriter](https://packagist.org/packages/avadim/fast-excel-writer))
4+
5+
## Introduction
6+
7+
This library is a wrapper for avadim/fast-excel-writer, so it's also lightweight, fast, and requires a minimum of memory.
8+
Using this library, you can export arrays, collections and models to a XLSX-file from your Laravel application.
9+
10+
## Installation
11+
12+
Install via composer:
13+
14+
```
15+
composer require avadim/fast-excel-laravel
16+
```
17+
And then you can use facade ```Excel```
18+
19+
```php
20+
// Create workbook
21+
$excel = \Excel::create();
22+
// do something...
23+
// and save XLSX-file to default storage
24+
$excel->saveTo('path/file.xlsx');
25+
26+
// or save file to specified disk
27+
$excel->store('disk', 'path/file.xlsx');
28+
```
29+
Export a Model
30+
```php
31+
32+
// Create workbook with sheet named 'Users'
33+
$excel = \Excel::create('Users');
34+
35+
// Write all users to Excel file
36+
$sheet->writeModel(Users::class);
37+
38+
// Write users with field names in the first row
39+
$sheet->withHeaders()
40+
->applyFontStyleBold()
41+
->applyBorder('thin')
42+
->writeModel(Users::class);
43+
44+
$excel->saveTo('path/file.xlsx');
45+
```
46+
47+
Export any collections and arrays
48+
```php
49+
// Create workbook with sheet named 'Users'
50+
$excel = \Excel::create('Users');
51+
52+
$sheet = $excel->getSheet();
53+
$users = User::all();
54+
$sheet->writeData($users);
55+
56+
$sheet = $excel->makeSheet('Records');
57+
$records = \DB::table('users')->where('age', '>=', 21)->get(['id', 'name', 'birthday']);
58+
$sheet->writeData($records);
59+
60+
$sheet = $excel->makeSheet('Collection');
61+
$collection = collect([
62+
[ 'id' => 1, 'site' => 'google.com' ],
63+
[ 'id' => 2, 'site.com' => 'youtube.com' ],
64+
]);
65+
$sheet->writeData($collection);
66+
67+
$sheet = $excel->makeSheet('Array');
68+
$array = [
69+
[ 'id' => 1, 'name' => 'Helen' ],
70+
[ 'id' => 2, 'name' => 'Peter' ],
71+
];
72+
$sheet->writeData($array);
73+
74+
$sheet = $excel->makeSheet('Callback');
75+
$sheet->writeData(function () {
76+
foreach (User::cursor() as $user) {
77+
yield $user;
78+
}
79+
});
80+
81+
```
82+
83+
## Advanced usage
84+
85+
See detailed documentation for avadim/fast-excel-laravel here: https://github.com/aVadim483/fast-excel-writer/tree/master#readme
86+
87+
```php
88+
$excel = \Excel::create('Users');
89+
$sheet = $excel->getSheet();
90+
91+
$sheet->setColWidth('B', 12);
92+
$sheet->setColOptions('c', ['width' => 12, 'text-align' => 'center']);
93+
$sheet->setColWidth('d', 'auto');
94+
95+
$title = 'This is demo of avadim/fast-excel-laravel';
96+
// begin area for direct access to cells
97+
$area = $sheet->beginArea();
98+
$area->setValue('A2:D2', $title)
99+
->applyFontSize(14)
100+
->applyFontStyleBold()
101+
->applyTextCenter();
102+
103+
$area
104+
->setValue('a4:a5', '#')
105+
->setValue('b4:b5', 'Number')
106+
->setValue('c4:d4', 'Movie Character')
107+
->setValue('c5', 'Birthday')
108+
->setValue('d5', 'Name')
109+
;
110+
111+
$area->withRange('a4:d5')
112+
->applyBgColor('#ccc')
113+
->applyFontStyleBold()
114+
->applyOuterBorder('thin')
115+
->applyInnerBorder('thick')
116+
->applyTextCenter();
117+
118+
$sheet->writeAreas();
119+
120+
$sheet->writeData($data);
121+
$excel->saveTo($testFileName);
122+
123+
```
124+
125+
## Do you want to support FastExcelLaravel?
126+
127+
if you find this package useful you can support and donate to me for a cup of coffee:
128+
129+
* USDT (TRC20) TSsUFvJehQBJCKeYgNNR1cpswY6JZnbZK7
130+
* USDT (ERC20) 0x5244519D65035aF868a010C2f68a086F473FC82b
131+
132+
Or just give me star on GitHub :)

composer.json

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,23 @@
1111
],
1212
"type": "library",
1313
"homepage": "https://github.com/aVadim483/fast-excel-laravel",
14+
"license": "MIT",
15+
"autoload": {
16+
"psr-4": {
17+
"avadim\\FastExcelLaravel\\": "./src/FastExcelLaravel"
18+
}
19+
},
1420
"require": {
1521
"php": "^7.4|^8.1",
22+
"ext-json": "*",
1623
"avadim/fast-excel-writer": "^4.0",
1724
"illuminate/support": "^6.0|^7.0|^8.0|^9.0|^10.0",
1825
"illuminate/contracts": "^6.0|^7.0|^8.0|^9.0|^10.0"
1926
},
2027
"require-dev": {
2128
"orchestra/testbench": "^4.0|^5.0|^6.0",
22-
"phpunit/phpunit": "^8.0|^9.0"
23-
},
24-
"license": "MIT",
25-
"autoload": {
26-
"psr-4": {
27-
"avadim\\FastExcelLaravel\\": "./src/FastExcelLaravel"
28-
}
29+
"phpunit/phpunit": "^8.0|^9.0",
30+
"avadim/fast-excel-reader": "^2.2"
2931
},
3032
"extra": {
3133
"laravel": {

src/FastExcelLaravel/Excel.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ public function __construct(?array $options = [])
99

1010
}
1111

12-
public static function create($sheets = null, ?array $options = [])
12+
public static function create($sheets = null, ?array $options = []): ExcelWriter
1313
{
14-
return ExcelWriter::create($sheets);
14+
return ExcelWriter::create($sheets, $options);
1515
}
1616
}

src/FastExcelLaravel/ExcelWriter.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,18 @@ public static function create($sheets = null, ?array $options = []): ExcelWriter
2727
}
2828
}
2929
$excel = new self($options);
30-
if (is_array($sheets)) {
31-
foreach ($sheets as $sheetName) {
32-
$excel->makeSheet($sheetName);
30+
if ($sheets) {
31+
if (is_array($sheets)) {
32+
foreach ($sheets as $sheetName) {
33+
$excel->makeSheet($sheetName);
34+
}
35+
}
36+
else {
37+
$excel->makeSheet((string)$sheets);
3338
}
3439
}
3540
else {
36-
$excel->makeSheet($sheets);
41+
$excel->makeSheet();
3742
}
3843

3944
return $excel;

src/FastExcelLaravel/SheetWriter.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ protected function _writeHeader($record)
4444

4545
public function writeRow(array $rowValues = [], array $rowStyle = null, array $cellStyles = null): Sheet
4646
{
47-
if ($this->dataRowCount > 0 && $this->headers['header_keys']) {
47+
if ($this->dataRowCount > 0 && !empty($this->headers['header_keys'])) {
4848
$rowData = [];
4949
foreach ($this->headers['header_keys'] as $key) {
5050
if (isset($rowValues[$key])) {
@@ -131,6 +131,7 @@ public function withHeaders(?array $headers = [], ?array $rowStyle = [], ?array
131131
'rowStyle' => $rowStyle,
132132
'colStyles' => $colStyles,
133133
];
134+
$this->lastTouch['ref'] = 'row';
134135

135136
return $this;
136137
}

0 commit comments

Comments
 (0)