Skip to content

Commit c1fe5be

Browse files
authored
Merge pull request #259 from pxlrbt/feat/csv-settings
Add support for CsvSettings
2 parents d8f5719 + 89bc33c commit c1fe5be

File tree

5 files changed

+96
-1
lines changed

5 files changed

+96
-1
lines changed

readme.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,39 @@ ExportAction::make()->exports([
172172
```
173173

174174

175+
### CSV settings
176+
177+
When exporting as CSV, you can customize the CSV output via `->withCsvSettings()` using the `CsvSettings` DTO. Any setting left as `null` will use the Laravel Excel config defaults.
178+
179+
```php
180+
use pxlrbt\FilamentExcel\Actions\Tables\ExportAction;
181+
use pxlrbt\FilamentExcel\Exports\ExcelExport;
182+
use pxlrbt\FilamentExcel\Exports\CsvSettings;
183+
use Maatwebsite\Excel\Excel;
184+
185+
ExportAction::make()->exports([
186+
ExcelExport::make()
187+
->withWriterType(Excel::CSV)
188+
->withCsvSettings(new CsvSettings(
189+
delimiter: ';',
190+
useBom: true,
191+
)),
192+
])
193+
```
194+
195+
You can also pass a Closure for dynamic configuration:
196+
197+
```php
198+
ExcelExport::make()
199+
->withWriterType(Excel::CSV)
200+
->withCsvSettings(fn () => new CsvSettings(
201+
delimiter: '\t',
202+
outputEncoding: 'ISO-8859-1',
203+
))
204+
```
205+
206+
Available settings: `delimiter`, `enclosure`, `lineEnding`, `useBom`, `includeSeparatorLine`, `excelCompatibility`, `outputEncoding`.
207+
175208
### Defining columns
176209

177210
When using `->fromForm()`/`->fromTable()`/`->fromModel()` the columns are resolved from your table or form definition. You can also provide columns manually, append columns or overwrite generated columns.

src/Exports/Concerns/CanQueue.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ protected function prepareQueuedExport()
5151
$this->writerType = $this->getWriterType();
5252
$this->columnFormats = $this->getColumnFormats();
5353
$this->columnWidths = $this->getColumnWidths();
54+
$this->csvSettings = $this->getCsvSettings();
5455
$this->livewireClass = $this->getLivewireClass();
5556

5657
// Reset
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace pxlrbt\FilamentExcel\Exports\Concerns;
4+
5+
use Closure;
6+
use pxlrbt\FilamentExcel\Exports\CsvSettings;
7+
8+
trait WithCsvSettings
9+
{
10+
protected Closure|CsvSettings|array|null $csvSettings = null;
11+
12+
public function withCsvSettings(Closure|CsvSettings $csvSettings): static
13+
{
14+
$this->csvSettings = $csvSettings;
15+
16+
return $this;
17+
}
18+
19+
public function getCsvSettings(): array
20+
{
21+
$settings = $this->evaluate($this->csvSettings);
22+
23+
if ($settings instanceof CsvSettings) {
24+
return $settings->toArray();
25+
}
26+
27+
return $settings ?? [];
28+
}
29+
}

src/Exports/CsvSettings.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace pxlrbt\FilamentExcel\Exports;
4+
5+
readonly class CsvSettings
6+
{
7+
public function __construct(
8+
public ?string $delimiter = null,
9+
public ?string $enclosure = null,
10+
public ?string $lineEnding = null,
11+
public ?bool $useBom = null,
12+
public ?bool $includeSeparatorLine = null,
13+
public ?bool $excelCompatibility = null,
14+
public ?string $outputEncoding = null,
15+
) {}
16+
17+
public function toArray(): array
18+
{
19+
return array_filter([
20+
'delimiter' => $this->delimiter,
21+
'enclosure' => $this->enclosure,
22+
'line_ending' => $this->lineEnding,
23+
'use_bom' => $this->useBom,
24+
'include_separator_line' => $this->includeSeparatorLine,
25+
'excel_compatibility' => $this->excelCompatibility,
26+
'output_encoding' => $this->outputEncoding,
27+
], fn ($value) => $value !== null);
28+
}
29+
}

src/Exports/ExcelExport.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Maatwebsite\Excel\Concerns\WithColumnFormatting;
1818
use Maatwebsite\Excel\Concerns\WithColumnWidths;
1919
use Maatwebsite\Excel\Concerns\WithCustomChunkSize;
20+
use Maatwebsite\Excel\Concerns\WithCustomCsvSettings;
2021
use Maatwebsite\Excel\Concerns\WithEvents;
2122
use Maatwebsite\Excel\Concerns\WithHeadings as HasHeadings;
2223
use Maatwebsite\Excel\Concerns\WithMapping as HasMapping;
@@ -33,6 +34,7 @@
3334
use pxlrbt\FilamentExcel\Exports\Concerns\WithChunkSize;
3435
use pxlrbt\FilamentExcel\Exports\Concerns\WithColumnFormats;
3536
use pxlrbt\FilamentExcel\Exports\Concerns\WithColumns;
37+
use pxlrbt\FilamentExcel\Exports\Concerns\WithCsvSettings;
3638
use pxlrbt\FilamentExcel\Exports\Concerns\WithFilename;
3739
use pxlrbt\FilamentExcel\Exports\Concerns\WithHeadings;
3840
use pxlrbt\FilamentExcel\Exports\Concerns\WithMapping;
@@ -43,7 +45,7 @@
4345
use pxlrbt\FilamentExcel\Interactions\AskForWriterType;
4446
use pxlrbt\FilamentExcel\Jobs\Middleware\SetAuthenticatedUser;
4547

46-
class ExcelExport implements FromQuery, HasHeadings, HasMapping, ShouldAutoSize, WithColumnFormatting, WithColumnWidths, WithCustomChunkSize, WithEvents, WithMultipleSheets, WithTitle
48+
class ExcelExport implements FromQuery, HasHeadings, HasMapping, ShouldAutoSize, WithColumnFormatting, WithColumnWidths, WithCustomChunkSize, WithCustomCsvSettings, WithEvents, WithMultipleSheets, WithTitle
4749
{
4850
use AskForFilename;
4951
use AskForWriterType;
@@ -63,6 +65,7 @@ class ExcelExport implements FromQuery, HasHeadings, HasMapping, ShouldAutoSize,
6365
use WithChunkSize;
6466
use WithColumnFormats;
6567
use WithColumns;
68+
use WithCsvSettings;
6669
use WithFilename;
6770
use WithHeadings;
6871
use WithMapping;

0 commit comments

Comments
 (0)