BatchExportService.php
3.8 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
158
159
160
161
162
163
164
165
166
167
<?php
namespace App\Services;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Storage;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
/**
* 批量导出
* Class BatchExportService
* @package App\Services
* @author zbj
* @date 2023/5/8
*/
class BatchExportService
{
private $name = '';
/**
* @var array
*/
private $data = [];
/**
* @var null | \PhpOffice\PhpSpreadsheet\Spreadsheet;
*/
private $spreadsheet = null;
/**
* 包头与字段映射
* @var array
*/
private $head = [];
public function __construct($name)
{
$this->name = $name;
$this->spreadsheet = new Spreadsheet();
$this->spreadsheet->setActiveSheetIndex(0);
}
/**
* config hook
* @param $callback
* @return $this
* @throws \PhpOffice\PhpSpreadsheet\Exception
*/
public function config($callback)
{
if(is_callable($callback)) {
$callback($this->spreadsheet->getActiveSheet(),$this->spreadsheet);
}
return $this;
}
/**
* 设置表头
* @param array $data
* @return $this
*/
public function head($map = [])
{
$this->head = $map;
return $this;
}
/**
* @param $data
* @return $this
*/
public function data($data)
{
$this->data = $data;
return $this;
}
/**
* @return false|string
* @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
* @author zbj
* @date 2023/5/8
*/
public function save()
{
$file = "export/".date('Y-m-d')."/{$this->name}_" . date('YmdHis') . ".xlsx";
$filename = public_path('runtime/'.$file);
if (!is_dir(dirname($filename))) {
@mkdir(dirname($filename),0755, true);
}
$this->render();
$writer = new Xlsx($this->spreadsheet);
$writer->save($filename);
if(file_exists($filename) && filesize($filename)) {
return $file;
}
return false;
}
/**
* 进行数据渲染
* 遍历所有的单元格处理文件,进行数据处理
* @return $this
* @author zbj
* @date 2023/5/8
*/
protected function render()
{
$map = [];
$sheet = $this->spreadsheet->getActiveSheet();
$i = 0;
foreach ($this->head as $k=>$title) {
$chr = chr(65+$i);
$map[$chr] = $k;
if(is_array($title)) {
$title = $title[0];
}
$sheet->setCellValue($chr . "1", $title);
$i++;
}
foreach ($this->data as $k=>$item) {
foreach ($map as $chr => $field) {
if(is_array($this->head[$field]) && is_callable($this->head[$field][1])) { //TODO 进行字段内容的处理
$this->head[$field][1]($sheet, $chr . ($k + 2), $item[$field]);
}
$sheet->setCellValue($chr . ($k + 2), $this->getDataValue($field, $item));
}
}
return $this;
}
/**
* 读取指定字段的value, 支持"."形式指定多维数组
* @param $name
* @param $data
* @return mixed|null
* @author zbj
* @date 2023/5/8
*/
protected function getDataValue($name, $data)
{
if(false === strpos($name, '.')) {
return $data[$name] ?? null;
}
$name = explode('.', $name);
$name[0] = strtolower($name[0]);
// 按.拆分成多维数组进行判断
foreach ($name as $val) {
if (isset($data[$val])) {
$data = $data[$val];
} else {
$data = null;
break;
}
}
return $data;
}
}