Skip to content

Commit

Permalink
Merge pull request #12 from luanfreitasdev/feature/test_spout
Browse files Browse the repository at this point in the history
add box spout to generate xlsx / csv
  • Loading branch information
luanfreitasdev committed May 6, 2021
2 parents c232ac1 + ac54614 commit ba36a3d
Show file tree
Hide file tree
Showing 9 changed files with 292 additions and 195 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@
}
},
"require": {
"php": "^7.4|^8.0",
"php": "^7.4|^8",
"livewire/livewire": "^2.4",
"shuchkin/simplexlsxgen": "^0.9.25"
"box/spout": "^3.2"
},
"require-dev": {
"phpunit/phpunit": "^9.5"
Expand Down
1 change: 1 addition & 0 deletions resources/views/tailwind/2/header.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class="block px-4 py-2 text-gray-800 hover:bg-gray-100 hover:text-black-200">Exc
<a wire:click="exportToCsv()" href="#"
class="block px-4 py-2 text-gray-800 hover:bg-gray-100 hover:text-black-200">Csv</a>
</div>

</div>

<div style="min-width: 60px;">
Expand Down
5 changes: 3 additions & 2 deletions src/PowerGridComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
use Livewire\Component;
use Livewire\WithPagination;
use PowerComponents\LivewirePowerGrid\Helpers\Collection;
use PowerComponents\LivewirePowerGrid\Services\ExportToCsv;
use PowerComponents\LivewirePowerGrid\Services\ExportToXLS;
use PowerComponents\LivewirePowerGrid\Services\Spout\ExportToCsv;
use PowerComponents\LivewirePowerGrid\Services\Spout\ExportToXLS;
use PowerComponents\LivewirePowerGrid\Traits\Checkbox;
use PowerComponents\LivewirePowerGrid\Traits\Filter;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
Expand Down Expand Up @@ -330,6 +330,7 @@ public function exportToExcel(): BinaryFileResponse
->fromCollection($this->columns(), $this->collection())
->withCheckedRows(array_merge($this->checkbox_values, $this->filtered))
->download();

}

/**
Expand Down
58 changes: 29 additions & 29 deletions src/Services/Export.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,59 +13,59 @@ class Export
public array $columns;
public array $checked_values;

public function fileName(string $name): Export
{
$this->fileName = $name;
return $this;
}

public function fromCollection(array $columns, Collection $collection): Export
{
$this->columns = $columns;
$this->collection = $collection;
return $this;
}

public function withCheckedRows($checked_values): Export
{
$this->checked_values = $checked_values;
return $this;
}

/**
* @throws Exception
*/
public function prepare(Collection $collection, array $columns, array $checkedValues): array
{
$header = [];
$title = collect();

$header = collect();

if (count($checkedValues)) {
$collection = $collection->whereIn('id', $checkedValues);
}

$collection = $collection->map(function ($row) use ($columns, $title) {
$collection = $collection->map(function ($row) use ($columns, $header) {
$item = collect();
collect($columns)->each(function ($column) use ($row, $title, $item) {
collect($columns)->each(function ($column) use ($row, $header, $item) {
if ($column->hidden === false && $column->visible_in_export === true) {
foreach ($row as $key => $value) {
if ($key === $column->field) {
$item->put($column->title, $value);
}
}
if (!$title->contains($column->title)) {
$title->push($column->title);
if (!$header->contains($column->title)) {
$header->push($column->title);
}

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

$header[] = $title->toArray();

return array_merge($header, $collection->toArray());
return [
'headers' => $header->toArray(),
'rows' => $collection->toArray()
];
}

public function fileName(string $name): Export
{
$this->fileName = $name;
return $this;
}

public function fromCollection(array $columns, Collection $collection): Export
{
$this->columns = $columns;
$this->collection = $collection;
return $this;
}

public function withCheckedRows($checked_values): Export
{
$this->checked_values = $checked_values;
return $this;
}


}
34 changes: 0 additions & 34 deletions src/Services/ExportToCsv.php

This file was deleted.

34 changes: 0 additions & 34 deletions src/Services/ExportToXLS.php

This file was deleted.

46 changes: 46 additions & 0 deletions src/Services/Spout/ExportToCsv.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php


namespace PowerComponents\LivewirePowerGrid\Services\Spout;

use Box\Spout\Common\Entity\Style\CellAlignment;
use Box\Spout\Common\Entity\Style\Color;
use Box\Spout\Writer\Common\Creator\Style\StyleBuilder;
use Box\Spout\Writer\Common\Creator\WriterEntityFactory;
use PowerComponents\LivewirePowerGrid\Services\Contracts\ExportInterface;
use PowerComponents\LivewirePowerGrid\Services\Export;

class ExportToCsv extends Export implements ExportInterface
{

public function download()
{

$this->build();

return response()
->download(storage_path($this->fileName . '.csv'));

}

public function build()
{

$data = $this->prepare($this->collection, $this->columns, $this->checked_values);

$writer = WriterEntityFactory::createCSVWriter();
$writer->openToFile(storage_path($this->fileName . '.csv'));

$row = WriterEntityFactory::createRowFromArray($data['headers']);

$writer->addRow($row);

foreach ($data['rows'] as $row) {
$row = WriterEntityFactory::createRowFromArray($row);
$writer->addRow($row);
}

$writer->close();

}
}
53 changes: 53 additions & 0 deletions src/Services/Spout/ExportToXLS.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php


namespace PowerComponents\LivewirePowerGrid\Services\Spout;

use Box\Spout\Common\Entity\Style\CellAlignment;
use Box\Spout\Common\Entity\Style\Color;
use Box\Spout\Writer\Common\Creator\Style\StyleBuilder;
use Box\Spout\Writer\Common\Creator\WriterEntityFactory;
use PowerComponents\LivewirePowerGrid\Services\Contracts\ExportInterface;
use PowerComponents\LivewirePowerGrid\Services\Export;

class ExportToXLS extends Export implements ExportInterface
{

public function download()
{

$this->build();

return response()
->download(storage_path($this->fileName . '.xlsx'));

}

public function build()
{
$data = $this->prepare($this->collection, $this->columns, $this->checked_values);

$writer = WriterEntityFactory::createXLSXWriter();
$writer->openToFile(storage_path($this->fileName . '.xlsx'));

$style = (new StyleBuilder())
->setFontBold()
->setFontColor(Color::BLACK)
->setShouldWrapText(false)
->setCellAlignment(CellAlignment::CENTER)
->setBackgroundColor('d0d3d8')
->build();

$row = WriterEntityFactory::createRowFromArray($data['headers'], $style);

$writer->addRow($row);

foreach ($data['rows'] as $row) {
$row = WriterEntityFactory::createRowFromArray($row);
$writer->addRow($row);
}

$writer->close();

}
}
Loading

0 comments on commit ba36a3d

Please sign in to comment.