Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
fail-fast: true
matrix:
os: [ ubuntu-latest ]
php: [ 8.1, 8.2 ]
php: [ 8.2 ]
stability: [ lowest, highest ]

steps:
Expand Down
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
<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)
[![Version](https://poser.pugx.org/smart145/fast-excel/version?format=flat)](https://packagist.org/packages/smart145/fast-excel)
[![License](https://poser.pugx.org/smart145/fast-excel/license?format=flat)](https://packagist.org/packages/smart145/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)
[![Tests](https://github.com/smart145/fast-excel/actions/workflows/tests.yml/badge.svg)](https://github.com/smart145/fast-excel/actions/workflows/tests.yml)
[![Total Downloads](https://poser.pugx.org/smart145/fast-excel/downloads)](https://packagist.org/packages/smart145/fast-excel)

Fast Excel import/export for Laravel, thanks to [Spout](https://github.com/box/spout).
See [benchmarks](#benchmarks) below.
Expand All @@ -16,13 +16,13 @@ See [benchmarks](#benchmarks) below.
Install via composer:

```
composer require rap2hpoutre/fast-excel
composer require smart145/fast-excel
```

Export a Model to `.xlsx` file:

```php
use Rap2hpoutre\FastExcel\FastExcel;
use Smart145\FastExcel\FastExcel;
use App\User;

// Load users
Expand Down Expand Up @@ -100,7 +100,7 @@ $users = (new FastExcel)->import('file.xlsx', function ($line) {
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,
'FastExcel' => Smart145\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.
Expand Down
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "rap2hpoutre/fast-excel",
"name": "smart145/fast-excel",
"type": "library",
"keywords": [
"laravel",
Expand All @@ -21,21 +21,21 @@
},
"autoload": {
"psr-4": {
"Rap2hpoutre\\FastExcel\\": "src/"
"Smart145\\FastExcel\\": "src/"
},
"files": [
"src/functions/fastexcel.php"
]
},
"autoload-dev": {
"psr-4": {
"Rap2hpoutre\\FastExcel\\Tests\\": "tests/"
"Smart145\\FastExcel\\Tests\\": "tests/"
}
},
"extra": {
"laravel": {
"providers": [
"Rap2hpoutre\\FastExcel\\Providers\\FastExcelServiceProvider"
"Smart145\\FastExcel\\Providers\\FastExcelServiceProvider"
]
}
},
Expand Down
61 changes: 54 additions & 7 deletions src/Exportable.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
<?php

namespace Rap2hpoutre\FastExcel;
namespace Smart145\FastExcel;

use Generator;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use InvalidArgumentException;
use OpenSpout\Common\Entity\Row;
use OpenSpout\Common\Entity\Style\Style;
use OpenSpout\Writer\Common\AbstractOptions;
use OpenSpout\Writer\Common\Creator\WriterEntityFactory;
use OpenSpout\Writer\XLSX\Writer;

/**
* Trait Exportable.
Expand All @@ -20,18 +20,23 @@
*/
trait Exportable
{
/**
* @var array
*/
private $columns_width;

/**
* @var Style
*/
private $header_style;
private $rows_style;

/**
* @param AbstractOptions $options
* @param \OpenSpout\Reader\ReaderInterface|\OpenSpout\Writer\WriterInterface $reader_or_writer
*
* @return mixed
*/
abstract protected function setOptions(&$options);
abstract protected function setOptions(&$reader_or_writer);

/**
* @param string $path
Expand Down Expand Up @@ -67,7 +72,7 @@ public function download($path, callable $callback = null)
if (method_exists(response(), 'streamDownload')) {
return response()->streamDownload(function () use ($path, $callback) {
self::exportOrDownload($path, 'openToBrowser', $callback);
}, $path);
});
}
self::exportOrDownload($path, 'openToBrowser', $callback);

Expand Down Expand Up @@ -124,6 +129,7 @@ private function exportOrDownload($path, $function, callable $callback = null)
$writer->addNewSheetAndMakeItCurrent();
}
}

$writer->close();
}

Expand Down Expand Up @@ -174,12 +180,22 @@ private function writeRowsFromCollection($writer, Collection $collection, ?calla
// createRowFromArray works only with arrays
if (!is_array($collection->first())) {
$collection = $collection->map(function ($value) {
if (is_object($value) && !($value instanceof Collection)) {
return (array) $value;
}

return $value->toArray();
});
}

// is_array($first_row) ? $first_row : $first_row->toArray())
$all_rows = $collection->map(function ($value) {
$first_row = $collection->first();
$removableKeys = array_filter(array_keys($first_row ?? []), fn ($key) => str_starts_with($key, '_'));
$all_rows = $collection->map(function ($value) use ($removableKeys) {
foreach ($removableKeys as $toRemove) {
unset($value[$toRemove]);
}

return Row::fromValues($value);
})->toArray();
if ($this->rows_style) {
Expand Down Expand Up @@ -235,11 +251,30 @@ private function writeHeader($writer, $first_row)
return;
}

$keys = array_keys(is_array($first_row) ? $first_row : $first_row->toArray());
$keys = $this->hasColumnsHeader()
? array_keys($this->columns_width)
: array_keys(is_array($first_row) ? $first_row : $first_row->toArray());

$writer->addRow(Row::fromValues($keys, $this->header_style));

if (!empty($this->columns_width)) {
$sheet = $writer->getCurrentSheet();

foreach (array_values($this->columns_width) as $key => $value) {
$sheet->setColumnWidth($value, $key + 1); // Excel start from column 1
}
}
// $writer->addRow(WriterEntityFactory::createRowFromArray($keys, $this->header_style));
}

/**
* @return bool
*/
private function hasColumnsHeader()
{
return $this->columns_width && \count($this->columns_width) && is_string(array_keys($this->columns_width)[0]);
}

/**
* Prepare collection by removing non string if required.
*/
Expand Down Expand Up @@ -307,4 +342,16 @@ public function rowsStyle(Style $style)

return $this;
}

/**
* @param $widths
*
* @return $this
*/
public function setColumnsWidth($widths)
{
$this->columns_width = $widths;

return $this;
}
}
18 changes: 9 additions & 9 deletions src/Facades/FastExcel.php
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
<?php

namespace Rap2hpoutre\FastExcel\Facades;
namespace Smart145\FastExcel\Facades;

use Illuminate\Support\Facades\Facade;

/**
* Class FastExcel.
*
* @method static \Rap2hpoutre\FastExcel\FastExcel data($data)
* @method static \Illuminate\Support\Collection import($path, callable $callback = null)
* @method static string export($path, callable $callback = null)
* @method static \Illuminate\Support\Collection importSheets($path, callable $callback = null)
* @method static \Rap2hpoutre\FastExcel\FastExcel configureCsv($delimiter = ',', $enclosure = '"', $encoding = 'UTF-8', $bom = false)
* @method static \Rap2hpoutre\FastExcel\FastExcel configureReaderUsing(?callable $callback = null)
* @method static \Rap2hpoutre\FastExcel\FastExcel configureWriterUsing(?callable $callback = null)
* @method static \Smart145\FastExcel\FastExcel data($data)
* @method static \Illuminate\Support\Collection import($path, callable $callback = null)
* @method static string export($path, callable $callback = null)
* @method static \Illuminate\Support\Collection importSheets($path, callable $callback = null)
* @method static \Smart145\FastExcel\FastExcel configureCsv($delimiter = ',', $enclosure = '"', $encoding = 'UTF-8', $bom = false)
* @method static \Smart145\FastExcel\FastExcel configureReaderUsing(?callable $callback = null)
* @method static \Smart145\FastExcel\FastExcel configureWriterUsing(?callable $callback = null)
*
* @see \Rap2hpoutre\FastExcel\FastExcel
* @see \Smart145\FastExcel\FastExcel
*/
class FastExcel extends Facade
{
Expand Down
2 changes: 1 addition & 1 deletion src/FastExcel.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Rap2hpoutre\FastExcel;
namespace Smart145\FastExcel;

use Generator;
use Illuminate\Support\Collection;
Expand Down
2 changes: 1 addition & 1 deletion src/Importable.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Rap2hpoutre\FastExcel;
namespace Smart145\FastExcel;

use Illuminate\Support\Collection;
use Illuminate\Support\Str;
Expand Down
4 changes: 2 additions & 2 deletions src/Providers/FastExcelServiceProvider.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Rap2hpoutre\FastExcel\Providers;
namespace Smart145\FastExcel\Providers;

use Illuminate\Support\ServiceProvider;

Expand Down Expand Up @@ -30,7 +30,7 @@ public function register()
$data = collect($data);
}

return new \Rap2hpoutre\FastExcel\FastExcel($data);
return new \Smart145\FastExcel\FastExcel($data);
});
}
}
2 changes: 1 addition & 1 deletion src/SheetCollection.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Rap2hpoutre\FastExcel;
namespace Smart145\FastExcel;

use Illuminate\Support\Collection;

Expand Down
4 changes: 2 additions & 2 deletions src/functions/fastexcel.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?php

use Rap2hpoutre\FastExcel\SheetCollection;
use Smart145\FastExcel\SheetCollection;

if (!function_exists('fastexcel')) {
/**
* Return app instance of FastExcel.
*
* @return Rap2hpoutre\FastExcel\FastExcel
* @return Smart145\FastExcel\FastExcel
*/
function fastexcel($data = null)
{
Expand Down
4 changes: 2 additions & 2 deletions tests/ChunkTest.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php

namespace Rap2hpoutre\FastExcel\Tests;
namespace Smart145\FastExcel\Tests;

use Rap2hpoutre\FastExcel\FastExcel;
use Smart145\FastExcel\FastExcel;

/**
* Class ChunkTest.
Expand Down
2 changes: 1 addition & 1 deletion tests/Dumb.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Rap2hpoutre\FastExcel\Tests;
namespace Smart145\FastExcel\Tests;

use Illuminate\Database\Eloquent\Model;

Expand Down
7 changes: 3 additions & 4 deletions tests/FastExcelTest.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
<?php

namespace Rap2hpoutre\FastExcel\Tests;
namespace Smart145\FastExcel\Tests;

use OpenSpout\Common\Entity\Style\Color;
use OpenSpout\Common\Entity\Style\Style;
use OpenSpout\Reader\XLSX\Options;
use Rap2hpoutre\FastExcel\FastExcel;
use Rap2hpoutre\FastExcel\SheetCollection;
use Smart145\FastExcel\FastExcel;
use Smart145\FastExcel\SheetCollection;

/**
* Class FastExcelTest.
Expand Down
6 changes: 3 additions & 3 deletions tests/IssuesTest.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php

namespace Rap2hpoutre\FastExcel\Tests;
namespace Smart145\FastExcel\Tests;

use Illuminate\Support\Collection;
use Rap2hpoutre\FastExcel\FastExcel;
use Rap2hpoutre\FastExcel\SheetCollection;
use Smart145\FastExcel\FastExcel;
use Smart145\FastExcel\SheetCollection;

/**
* Class IssuesTest.
Expand Down
2 changes: 1 addition & 1 deletion tests/TestCase.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Rap2hpoutre\FastExcel\Tests;
namespace Smart145\FastExcel\Tests;

use PHPUnit\Framework\TestCase as BaseTestCase;

Expand Down