|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Sfneal\ViewExport\Excel\Utils; |
|
4
|
|
|
|
|
5
|
|
|
use Maatwebsite\Excel\Facades\Excel; |
|
6
|
|
|
use Sfneal\ViewExport\Support\Adapters\ExcelExport; |
|
7
|
|
|
use Sfneal\ViewExport\Support\Adapters\Exporter; |
|
8
|
|
|
use Sfneal\ViewExport\Support\Interfaces\Downloadable; |
|
9
|
|
|
use Symfony\Component\HttpFoundation\BinaryFileResponse; |
|
10
|
|
|
|
|
11
|
|
|
class ExcelExporter extends Exporter implements Downloadable |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @var ExcelExport |
|
15
|
|
|
*/ |
|
16
|
|
|
private $excel; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* ExcelExporter constructor. |
|
20
|
|
|
* |
|
21
|
|
|
* @param ExcelExport $excel |
|
22
|
|
|
*/ |
|
23
|
|
|
public function __construct(ExcelExport $excel) |
|
24
|
|
|
{ |
|
25
|
|
|
$this->excel = $excel; |
|
26
|
|
|
|
|
27
|
|
|
// Only set output if the ExcelExport has a 'view' method |
|
28
|
|
|
if (method_exists($excel, 'view')) { |
|
29
|
|
|
$this->output = $excel->view()->render(); |
|
30
|
|
|
} |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Upload an Excel file to an AWS S3 file store. |
|
35
|
|
|
* |
|
36
|
|
|
* @param string $path |
|
37
|
|
|
* @return $this |
|
38
|
|
|
*/ |
|
39
|
|
|
public function upload(string $path): self |
|
40
|
|
|
{ |
|
41
|
|
|
$this->uploadPath = $path; |
|
42
|
|
|
Excel::store($this->excel, $path, 's3'); |
|
43
|
|
|
|
|
44
|
|
|
return $this; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Store an Excel on the local file system. |
|
49
|
|
|
* |
|
50
|
|
|
* @param string $storagePath |
|
51
|
|
|
* @return $this |
|
52
|
|
|
*/ |
|
53
|
|
|
public function store(string $storagePath): self |
|
54
|
|
|
{ |
|
55
|
|
|
$this->localPath = $storagePath; |
|
56
|
|
|
Excel::store($this->excel, $storagePath); |
|
57
|
|
|
|
|
58
|
|
|
return $this; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Download the exported view using the clients browser. |
|
63
|
|
|
* |
|
64
|
|
|
* @param string $filename |
|
65
|
|
|
* @return BinaryFileResponse |
|
66
|
|
|
*/ |
|
67
|
|
|
public function download(string $filename): BinaryFileResponse |
|
68
|
|
|
{ |
|
69
|
|
|
return Excel::download($this->excel, $filename); |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|