Skip to content

Commit fab5ce8

Browse files
committed
initial commit
1 parent 21e9801 commit fab5ce8

File tree

7 files changed

+361
-0
lines changed

7 files changed

+361
-0
lines changed

composer.json

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{
2+
"name": "avadim/fast-excel-laravel",
3+
"description": "Lightweight and very fast XLSX Excel Spreadsheet Writer for Laravel",
4+
"keywords": [
5+
"excel",
6+
"xlsx",
7+
"PHPExcel",
8+
"spreadsheet",
9+
"laravel",
10+
"export"
11+
],
12+
"type": "library",
13+
"homepage": "https://github.com/aVadim483/fast-excel-laravel",
14+
"require": {
15+
"php": "^7.4|^8.1",
16+
"avadim/fast-excel-writer": "^4.0",
17+
"illuminate/support": "^6.0|^7.0|^8.0|^9.0|^10.0",
18+
"illuminate/contracts": "^6.0|^7.0|^8.0|^9.0|^10.0"
19+
},
20+
"require-dev": {
21+
"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+
},
30+
"extra": {
31+
"laravel": {
32+
"providers": [
33+
"avadim\\FastExcelLaravel\\Providers\\ExcelServiceProvider"
34+
],
35+
"aliases": {
36+
"Excel": "avadim\\FastExcelLaravel\\Facades\\Excel"
37+
}
38+
}
39+
}
40+
}

src/FastExcelLaravel/Excel.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace avadim\FastExcelLaravel;
4+
5+
class Excel
6+
{
7+
public function __construct(?array $options = [])
8+
{
9+
10+
}
11+
12+
public static function create($sheets = null, ?array $options = [])
13+
{
14+
return ExcelWriter::create($sheets);
15+
}
16+
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?php
2+
3+
namespace avadim\FastExcelLaravel;
4+
5+
use Illuminate\Support\Collection;
6+
7+
class ExcelWriter extends \avadim\FastExcelWriter\Excel
8+
{
9+
/**
10+
* @param string|array $sheets
11+
* @param array|null $options
12+
*
13+
* @return ExcelWriter
14+
*/
15+
public static function create($sheets = null, ?array $options = []): ExcelWriter
16+
{
17+
if (empty($options['temp_dir'])) {
18+
$tempDir = storage_path('app/tmp/excel');
19+
if(!\File::isDirectory($tempDir)) {
20+
\File::makeDirectory($tempDir, 0777, true, true);
21+
}
22+
if (!$options) {
23+
$options = ['temp_dir' => $tempDir];
24+
}
25+
else {
26+
$options['temp_dir'] = $tempDir;
27+
}
28+
}
29+
$excel = new self($options);
30+
if (is_array($sheets)) {
31+
foreach ($sheets as $sheetName) {
32+
$excel->makeSheet($sheetName);
33+
}
34+
}
35+
else {
36+
$excel->makeSheet($sheets);
37+
}
38+
39+
return $excel;
40+
}
41+
42+
/**
43+
* @param string $sheetName
44+
*
45+
* @return SheetWriter
46+
*/
47+
public static function createSheet(string $sheetName): SheetWriter
48+
{
49+
return new SheetWriter($sheetName);
50+
}
51+
52+
/**
53+
* @param $data
54+
*
55+
* @return $this
56+
*/
57+
public function writeData($data): ExcelWriter
58+
{
59+
$this->getSheet()->writeData($data);
60+
61+
return $this;
62+
}
63+
64+
/**
65+
* Save file to local storage
66+
*
67+
* @param string $filePath
68+
*
69+
* @return bool
70+
*/
71+
public function saveTo(string $filePath): bool
72+
{
73+
$this->save(storage_path($filePath));
74+
75+
return true;
76+
}
77+
78+
/**
79+
* Store file to specified disk
80+
*
81+
* @param $disk
82+
* @param $path
83+
*
84+
* @return void
85+
*
86+
* @throws \Illuminate\Contracts\Filesystem\FileExistsException
87+
*/
88+
public function store($disk, $path)
89+
{
90+
$tmpFile = $this->writer->tempFilename();
91+
$this->save($tmpFile);
92+
$handle = fopen($tmpFile, 'rb');
93+
94+
\Storage::disk($disk)->writeStream($path, $handle);
95+
96+
fclose($handle);
97+
}
98+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace avadim\FastExcelLaravel\Facades;
4+
5+
use Illuminate\Support\Facades\Facade;
6+
7+
/**
8+
* Class Excel
9+
*
10+
* @method static string export($path, callable $callback = null)
11+
*/
12+
class Excel extends Facade
13+
{
14+
/**
15+
* Get the registered name of the component.
16+
*
17+
* @return string
18+
*/
19+
protected static function getFacadeAccessor()
20+
{
21+
return 'excel';
22+
}
23+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace avadim\FastExcelLaravel\Providers;
4+
5+
use Illuminate\Support\ServiceProvider;
6+
7+
class ExcelServiceProvider extends ServiceProvider
8+
{
9+
/**
10+
* Bootstrap any application services.
11+
*
12+
* @return void
13+
*/
14+
public function boot()
15+
{
16+
//
17+
}
18+
19+
/**
20+
* Register any application services.
21+
*
22+
* @SuppressWarnings("unused")
23+
*
24+
* @return void
25+
*/
26+
public function register()
27+
{
28+
$this->app->bind('excel', function ($app, $data = null) {
29+
if (is_array($data)) {
30+
$data = collect($data);
31+
}
32+
33+
return new \avadim\FastExcelLaravel\Excel();
34+
});
35+
}
36+
}
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
<?php
2+
3+
namespace avadim\FastExcelLaravel;
4+
5+
use avadim\FastExcelWriter\Sheet;
6+
use Illuminate\Support\Collection;
7+
8+
class SheetWriter extends Sheet
9+
{
10+
private ?array $headers = null;
11+
private int $dataRowCount = 0;
12+
13+
protected function _toArray($record)
14+
{
15+
if (is_object($record)) {
16+
if (method_exists($record, 'toArray')) {
17+
$result = $record->toArray();
18+
}
19+
else {
20+
$result = json_decode(json_encode($record), true);
21+
}
22+
}
23+
else {
24+
$result = (array)$record;
25+
}
26+
27+
return $result;
28+
}
29+
30+
protected function _writeHeader($record)
31+
{
32+
if (!$this->headers['header_keys']) {
33+
$this->headers['header_keys'] = array_keys($this->_toArray($record));
34+
}
35+
if (!$this->headers['header_values']) {
36+
$this->headers['header_values'] = $this->headers['header_keys'];
37+
}
38+
39+
$row = array_combine($this->headers['header_keys'], $this->headers['header_values']);
40+
$row = $this->headers['header_values'];
41+
$this->writeHeader($row, $this->headers['rowStyle'], $this->headers['colStyles']);
42+
++$this->dataRowCount;
43+
}
44+
45+
public function writeRow(array $rowValues = [], array $rowStyle = null, array $cellStyles = null): Sheet
46+
{
47+
if ($this->dataRowCount > 0 && $this->headers['header_keys']) {
48+
$rowData = [];
49+
foreach ($this->headers['header_keys'] as $key) {
50+
if (isset($rowValues[$key])) {
51+
$rowData[] = $rowValues[$key];
52+
}
53+
else {
54+
$rowData[] = null;
55+
}
56+
}
57+
}
58+
else {
59+
$rowData = array_values($rowValues);
60+
}
61+
return parent::writeRow($rowData, $rowStyle, $cellStyles);
62+
}
63+
64+
/**
65+
* @param $data
66+
* @param array|null $rowStyle
67+
* @param array|null $cellStyles
68+
*
69+
* @return $this
70+
*/
71+
public function writeData($data, array $rowStyle = null, array $cellStyles = null): SheetWriter
72+
{
73+
if (is_array($data) || ($data instanceof Collection)) {
74+
foreach ($data as $record) {
75+
if ($this->dataRowCount === 0 && $this->headers) {
76+
$this->_writeHeader($record);
77+
}
78+
$this->writeRow($this->_toArray($record), $rowStyle, $cellStyles);
79+
++$this->dataRowCount;
80+
}
81+
}
82+
elseif (is_callable($data)) {
83+
foreach ($data() as $record) {
84+
if ($this->dataRowCount === 0 && $this->headers) {
85+
$this->_writeHeader($record);
86+
}
87+
$this->writeRow($this->_toArray($record), $rowStyle, $cellStyles);
88+
++$this->dataRowCount;
89+
}
90+
}
91+
92+
return $this;
93+
}
94+
95+
/**
96+
* @param $model
97+
* @param array|null $rowStyle
98+
* @param array|null $cellStyles
99+
*
100+
* @return $this
101+
*/
102+
public function writeModel($model, array $rowStyle = null, array $cellStyles = null): SheetWriter
103+
{
104+
$this->writeData(static function() use ($model) {
105+
foreach ($model::cursor() as $user) {
106+
yield $user;
107+
}
108+
}, $rowStyle, $cellStyles);
109+
110+
return $this;
111+
}
112+
113+
public function withHeaders(?array $headers = [], ?array $rowStyle = [], ?array $colStyles = []): SheetWriter
114+
{
115+
$headerKeys = $headerValues = [];
116+
if ($headers) {
117+
foreach ($headers as $key => $val) {
118+
if (is_string($key)) {
119+
$headerKeys[] = $key;
120+
$headerValues[] = $val;
121+
}
122+
else {
123+
$headerKeys[] = $headerValues[] = $val;
124+
}
125+
}
126+
}
127+
128+
$this->headers = [
129+
'header_keys' => $headerKeys,
130+
'header_values' => $headerValues,
131+
'rowStyle' => $rowStyle,
132+
'colStyles' => $colStyles,
133+
];
134+
135+
return $this;
136+
}
137+
138+
}

src/autoload.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
spl_autoload_register(static function ($class) {
4+
$namespace = 'avadim\\FastExcelLaravel\\';
5+
if (0 === strpos($class, $namespace)) {
6+
include __DIR__ . '/FastExcelLaravel/' . str_replace($namespace, '', $class) . '.php';
7+
}
8+
});
9+
10+
// EOF

0 commit comments

Comments
 (0)