Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Elminson 0000 with custom header #164

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
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
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,31 @@ You can also import a specific sheet by its number:
$users = (new FastExcel)->sheet(3)->import('file.xlsx');
```

### Export large collections with chunk
#### ->withCustomHeader
#### ->setCustomHeader

- combine this 2 methods and you can set header on collection before export

```php

$collectionsOriginal = [
collect([
['col1' => 'row1 col1', 'col2' => 'row1 col2', 'col3' => 'row1 col3'],
['col1' => 'row2 col1', 'col2' => 'row2 col2', 'col3' => 'row2 col3'],
['col1' => 'row3 col1', 'col2' => 'row3 col2', 'col3' => 'row3 col3'],
])
];

$file = __DIR__.'/test_custom_header.xlsx';
$sheets = new SheetCollection($collectionsOriginal);
(new FastExcel($sheets))->withCustomHeader(true)
->setCustomHeader([
'Id',
'Nombre',
'Telefono'
])->export($file);
```
### Export large collections with chunk

Export rows one by one to avoid `memory_limit` issues [using `yield`](https://www.php.net/manual/en/language.generators.syntax.php):
Expand Down
46 changes: 45 additions & 1 deletion src/Exportable.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ trait Exportable
*/
private $header_style;

/**
* @var bool with Custom Header
*/
private $withCustomHeader;

/**
* @var array header Fields
*/
private $headerFields;

/**
* @param string $path
*
Expand Down Expand Up @@ -173,7 +183,15 @@ private function writeHeader($writer, $first_row)
return;
}

$keys = array_keys(is_array($first_row) ? $first_row : $first_row->toArray());
if ($this->withCustomHeader) {
$keys = array_values(is_array($this->headerFields) ? $this->headerFields : $this->headerFields->toArray());
if (count($first_row) != count($keys)) {
throw new InvalidArgumentException('setCustomHeader fields need to match with the number of rows');
}
} else {
$keys = array_keys(is_array($first_row) ? $first_row : $first_row->toArray());
}

if ($this->header_style) {
$writer->addRowWithStyle($keys, $this->header_style);
} else {
Expand Down Expand Up @@ -236,4 +254,30 @@ public function headerStyle(Style $style)

return $this;
}

/**
* @param $headerFields
*
* @return Exportable
*/
public function setCustomHeader($headerFields)
{
if ($this->withCustomHeader) {
$this->headerFields = $headerFields;
}

return $this;
}

/**
* @param bool $withCustomHeader
*
* @return Exportable
*/
public function withCustomHeader(bool $withCustomHeader)
{
$this->withCustomHeader = $withCustomHeader;

return $this;
}
}
42 changes: 42 additions & 0 deletions tests/FastExcelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,4 +190,46 @@ public function testExportWithHeaderStyle()

unlink($file);
}

/**
* @throws \Box\Spout\Common\Exception\IOException
* @throws \Box\Spout\Common\Exception\InvalidArgumentException
* @throws \Box\Spout\Common\Exception\UnsupportedTypeException
* @throws \Box\Spout\Reader\Exception\ReaderNotOpenedException
* @throws \Box\Spout\Writer\Exception\WriterNotOpenedException
*/
public function testExportWithCustomHeader()
{
$collectionsOriginal = [
collect([
['col1' => 'row1 col1', 'col2' => 'row1 col2', 'col3' => 'row1 col3'],
['col1' => 'row2 col1', 'col2' => 'row2 col2', 'col3' => 'row2 col3'],
['col1' => 'row3 col1', 'col2' => 'row3 col2', 'col3' => 'row3 col3'],
]),
];
$collectionsExpected = [
collect([
['Id' => 'row1 col1', 'Nombre' => 'row1 col2', 'Telefono' => 'row1 col3'],
['Id' => 'row2 col1', 'Nombre' => 'row2 col2', 'Telefono' => 'row2 col3'],
['Id' => 'row3 col1', 'Nombre' => 'row3 col2', 'Telefono' => 'row3 col3'],
]),
];
$file = __DIR__.'/test_custom_header.xlsx';
$sheets = new SheetCollection($collectionsOriginal);
(new FastExcel($sheets))->withCustomHeader(true)
->setCustomHeader([
'Id',
'Nombre',
'Telefono',
])->export($file);

$sheets = (new FastExcel())->importSheets($file);
$this->assertInstanceOf(SheetCollection::class, $sheets);

$this->assertEquals($collectionsExpected[0][0], $sheets->all()[0][0]);
$this->assertEquals($collectionsExpected[0][1], $sheets->all()[0][1]);
$this->assertEquals($collectionsExpected[0][2], $sheets->all()[0][2]);

unlink($file);
}
}