CustomExporter.php
4.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<?php
/**
* +-----------------------------------------------------------------------------------------------------------------------
* 扩展类:通用导出扩展类
* +-----------------------------------------------------------------------------------------------------------------------
*
* PHP version 7
*
* @category App\Admin\Extensions
* @package App\Admin\Extensions
* @author Richer <yangzi1028@163.com>
* @date 2021年8月30日10:57:09
* @copyright 2020-2022 Richer (http://www.Richer.com/)
* @license http://www.Richer.com/ License
* @link http://www.Richer.com/
*/
namespace App\Admin\Extensions;
use Encore\Admin\Grid;
use Encore\Admin\Grid\Exporters\ExcelExporter;
use Illuminate\Support\Arr;
/**
* Class CustomExporter
*
* @category App\Admin\Extensions
* @package App\Admin\Extensions
* @author Richer <yangzi1028@163.com>
* @date 2021年8月30日10:57:09
* @copyright 2020-2022 Richer (http://www.Richer.com/)
* @license http://www.Richer.com/ License
* @link http://www.Richer.com/
*/
class CustomExporter extends ExcelExporter
{
// protected $filename = 'table';
// protected $head = [];
// protected $body = [];
//
// public function __construct($filename = 'table', $head = null, $body = null, Grid $grid = null)
// {
// $this->filename = $filename;
// $this->head = $head;
// $this->body = $body;
// parent::__construct($grid);
// }
//
// /**
// * {@inheritdoc}
// */
// public function export()
// {
// $titles = [];
// $filename = $this->filename.'.xls';
// $data = $this->getData();
// if (!empty($data)) {
// $columns = Arr::dot($this->sanitize($data[0]));
// $titles = array_keys($columns);
// }
// $output = self::putcsv(($this->head == []) ? array_keys($columns) : $this->head);
//
// if ($this->body == []) {
// foreach ($data as $row) {
// $row = Arr::only($row, $titles);
//
// $output .= self::putcsv(Arr::dot($row));
// dump($output);
// }
// } else {
// foreach ($this->body as $row) {
// $output .= self::putcsv(Arr::dot($row));
// }
// }
// dd(1);
// $headers = [
// 'Content-Encoding' => 'UTF-8',
// 'Content-Type' => 'text/csv;charset=UTF-8',
// 'Content-Disposition' => "attachment; filename=\"$filename\"",
// ];
// response(rtrim($output, "\n"), 200, $headers)->send();
// exit;
// }
//
// /**
// * Remove indexed array.
// *
// * @param array $row
// *
// * @return array
// */
// protected function sanitize(array $row)
// {
// return collect($row)->reject(function ($val) {
// return is_array($val) && !Arr::isAssoc($val);
// })->toArray();
// }
//
// /**
// * @param $row
// * @param string $fd
// * @param string $quot
// *
// * @return string
// */
// protected static function putcsv($row, $fd = ',', $quot = '"')
// {
// $str = '';
// foreach ($row as $cell) {
// $cell = str_replace([$quot, "\n"], [$quot.$quot, ''], $cell);
// if (strstr($cell, $fd) !== false || strstr($cell, $quot) !== false) {
// $str .= $quot.$cell.$quot.$fd;
// } else {
// $str .= $cell.$fd;
// }
// }
// return substr($str, 0, -1)."\n";
// }
protected $fileName = '列表';
protected $columns = [];
protected $index = 0;
/**
* OrderExporter constructor.
*
* @param string $fileName
* @param array $columns
*/
public function __construct($fileName = '', $columns = [])
{
if ($fileName) {
$this->fileName = $fileName;
}
// $this->fileName .= '.csv';
$this->fileName .= '.xls';
if ($columns) {
$this->columns = $columns;
}
// $this->columns['id'] = '';
}
/**
* @return array
*/
public function headings(): array
{
// dump($this->columns);
// dump(array_values($this->columns));
if (!empty($this->columns)) {
return array_values($this->columns);
}
return $this->headings;
}
}