Skip to content

Commit fcd66ac

Browse files
authored
Add set sheet name (#25)
* Add set sheetname * Add property in livewire, update readme. * Fix blank request * Fix phpstan error * Fix error * refactor * Update readme, add substring * Update readme * Move substr of sheetname in job.
1 parent 844ab29 commit fcd66ac

File tree

5 files changed

+45
-3
lines changed

5 files changed

+45
-3
lines changed

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,23 @@ You can set the export type by setting the property to `csv` or `xlsx`. Default
9292
<livewire:export-button :table-id="$dataTable->getTableId()" type="csv" />
9393
```
9494

95+
## Set Excel Sheet Name
96+
97+
Option 1: You can set the Excel sheet name by setting the property.
98+
99+
```php
100+
<livewire:export-button :table-id="$dataTable->getTableId()" sheet-name="Monthly Report" />
101+
```
102+
103+
Option 2: You can also set the Excel sheet name by overwriting the method.
104+
105+
```php
106+
protected function sheetName() : string
107+
{
108+
return "Yearly Report";
109+
}
110+
```
111+
95112
## Formatting Columns
96113

97114
You can format the column by setting it via Column definition on you DataTable service class.

src/Jobs/DataTableExportJob.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use OpenSpout\Common\Type;
2222
use OpenSpout\Writer\Common\Creator\Style\StyleBuilder;
2323
use OpenSpout\Writer\Common\Creator\WriterEntityFactory;
24+
use OpenSpout\Writer\XLSX\Writer as XLSXWriter;
2425
use PhpOffice\PhpSpreadsheet\Shared\Date;
2526
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
2627
use Yajra\DataTables\Html\Column;
@@ -40,6 +41,8 @@ class DataTableExportJob implements ShouldQueue, ShouldBeUnique
4041

4142
public array $request;
4243

44+
public string $sheetName;
45+
4346
/**
4447
* @var int|string
4548
*/
@@ -51,13 +54,15 @@ class DataTableExportJob implements ShouldQueue, ShouldBeUnique
5154
* @param array $dataTable
5255
* @param array $request
5356
* @param int|string $user
57+
* @param string $sheetName
5458
*/
55-
public function __construct(array $dataTable, array $request, $user)
59+
public function __construct(array $dataTable, array $request, $user, string $sheetName = 'Sheet1')
5660
{
5761
$this->dataTable = $dataTable[0];
5862
$this->attributes = $dataTable[1];
5963
$this->request = $request;
6064
$this->user = $user;
65+
$this->sheetName = $sheetName;
6166
}
6267

6368
/**
@@ -99,6 +104,11 @@ public function handle()
99104
$writer = WriterEntityFactory::createWriter($type);
100105
$writer->openToFile($path);
101106

107+
if ($writer instanceof XLSXWriter) {
108+
$sheet = $writer->getCurrentSheet();
109+
$sheet->setName(substr($this->sheetName,0,31));
110+
}
111+
102112
$columns = $this->getExportableColumns($oTable);
103113
$writer->addRow(
104114
WriterEntityFactory::createRowFromArray(
@@ -166,6 +176,7 @@ public function handle()
166176

167177
$writer->addRow(WriterEntityFactory::createRow($cells));
168178
}
179+
169180
$writer->close();
170181
}
171182

src/Livewire/ExportButtonComponent.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ class ExportButtonComponent extends Component
2323

2424
public string $filename = '';
2525

26+
public string $sheetName = 'Sheet1';
27+
2628
public bool $exporting = false;
2729

2830
public bool $exportFinished = false;

src/WithExportQueue.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,23 @@ public function exportQueue(): string
4242
$job = new DataTableExportJob(
4343
[self::class, $this->attributes],
4444
request()->all(),
45-
Auth::id() ?? 0
45+
Auth::id() ?? 0,
46+
$this->sheetName(),
4647
);
4748

4849
$batch = Bus::batch([$job])->name('datatables-export')->dispatch();
4950

5051
return $batch->id;
5152
}
53+
54+
/**
55+
* Default sheet name.
56+
* Character limit 31.
57+
*
58+
* @return string
59+
*/
60+
protected function sheetName() : string
61+
{
62+
return request('sheetName', 'Sheet1');
63+
}
5264
}

src/resources/views/export-button.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
x-on:submit.prevent="
44
$refs.exportBtn.disabled = true;
55
var url = window._buildUrl(LaravelDataTables['{{ $tableId }}'], 'exportQueue');
6-
$.get(url + '&exportType={{$fileType}}').then(function(exportId) {
6+
$.get(url + '&exportType={{$fileType}}&sheetName={{$sheetName}}').then(function(exportId) {
77
$wire.export(exportId)
88
}).catch(function(error) {
99
$wire.exportFinished = true;

0 commit comments

Comments
 (0)