1 | <?php |
||
2 | /** |
||
3 | * @name: TWorklSheet |
||
4 | * @author: JiaMeng <[email protected]> |
||
5 | * @file: Export.php |
||
6 | * @Date: 2024/03/04 10:15 |
||
7 | */ |
||
8 | namespace tinymeng\spreadsheet\Excel; |
||
9 | |||
10 | use PhpOffice\PhpSpreadsheet\Cell\Coordinate; |
||
11 | use PhpOffice\PhpSpreadsheet\Exception; |
||
12 | use PhpOffice\PhpSpreadsheet\Worksheet\Drawing; |
||
13 | use tinymeng\tools\FileTool; |
||
14 | |||
15 | trait SpreadSheet{ |
||
16 | |||
17 | /** |
||
18 | * sheet名称 |
||
19 | * @var |
||
20 | */ |
||
21 | private $sheetName; |
||
22 | /** |
||
23 | * 定义所有字段 |
||
24 | * @var array |
||
25 | */ |
||
26 | private $field = []; |
||
27 | |||
28 | /** |
||
29 | * 文件信息 |
||
30 | * @var array |
||
31 | */ |
||
32 | private $fileTitle=[]; |
||
33 | |||
34 | /** |
||
35 | * 标题占用行数 |
||
36 | * @var int |
||
37 | */ |
||
38 | private $title_row = 1; |
||
39 | |||
40 | /** |
||
41 | * 左侧分组字段 |
||
42 | * @var array |
||
43 | */ |
||
44 | private $group_left = []; |
||
45 | |||
46 | |||
47 | /** |
||
48 | * 表头所在行 |
||
49 | * @var int |
||
50 | */ |
||
51 | public $titleFieldsRow = 1; |
||
52 | |||
53 | /** |
||
54 | * 获取表格列数 |
||
55 | * @var |
||
56 | */ |
||
57 | public $columnCount; |
||
58 | |||
59 | /** |
||
60 | * 获取表格行数 |
||
61 | * @var |
||
62 | */ |
||
63 | public $rowCount; |
||
64 | /** |
||
65 | * title |
||
66 | * @var |
||
67 | */ |
||
68 | public $title; |
||
69 | /** |
||
70 | * title字段 |
||
71 | * @var |
||
72 | */ |
||
73 | public $title_fields; |
||
74 | /** |
||
75 | * @var string[] |
||
76 | */ |
||
77 | private $cellName = []; |
||
78 | |||
79 | /** |
||
80 | * 文件中图片读取 |
||
81 | * 图片存储的相对路径 |
||
82 | * @var string |
||
83 | */ |
||
84 | public $relative_path = '/images'; |
||
85 | |||
86 | /** |
||
87 | * 文件中图片读取 |
||
88 | * 图片存储的绝对路径 |
||
89 | * @var string |
||
90 | */ |
||
91 | public $image_path = '/images'; |
||
92 | |||
93 | public function setTitle($title){ |
||
94 | $this->title = $title; |
||
95 | $this->getTitleFields(); |
||
96 | return $this; |
||
97 | } |
||
98 | |||
99 | /** |
||
100 | * @param $value |
||
101 | * @return $this |
||
102 | */ |
||
103 | public function setRelativePath($value){ |
||
104 | $this->relative_path = $value; |
||
105 | return $this; |
||
106 | } |
||
107 | |||
108 | /** |
||
109 | * @param $value |
||
110 | * @return $this |
||
111 | */ |
||
112 | public function setImagePath($value){ |
||
113 | $this->image_path = $value; |
||
114 | return $this; |
||
115 | } |
||
116 | |||
117 | /** |
||
118 | * getExcelData |
||
119 | * @param $this->title_fields |
||
120 | * @return array |
||
121 | * @author: Tinymeng <[email protected]> |
||
122 | * @time: 2022/2/22 11:30 |
||
123 | */ |
||
124 | public function getExcelData(){ |
||
125 | /* 循环读取每个单元格的数据 */ |
||
126 | $result = []; |
||
127 | $dataRow = $this->titleFieldsRow+1; |
||
128 | |||
129 | //行数循环 |
||
130 | for ($row = $dataRow; $row <= $this->rowCount; $row++){ |
||
131 | $rowFlog = false;//行是否有内容(过滤空行) |
||
132 | //列数循环 , 列数是以A列开始 |
||
133 | $data = []; |
||
134 | foreach ($this->cellName as $column){ |
||
135 | $cell = $this->workSheet->getCell($column.$row); |
||
136 | $value = trim($cell->getFormattedValue()); |
||
137 | if(isset($this->title_fields[$column])){ |
||
138 | $data[$this->title_fields[$column]] = $value; |
||
139 | if(!empty($value)) $rowFlog = true;//有内容 |
||
140 | } |
||
141 | } |
||
142 | if($rowFlog) $result[] = $data; |
||
143 | } |
||
144 | |||
145 | /* |
||
146 | * 读取表格图片数据 |
||
147 | * (如果为空右击图片转为浮动图片) |
||
148 | */ |
||
149 | $image_filename_prefix = time().rand(100,999).$this->sheet; |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
150 | foreach ($this->workSheet->getDrawingCollection() as $drawing) { |
||
151 | /**@var $drawing Drawing* */ |
||
152 | list($column, $row) = Coordinate::coordinateFromString($drawing->getCoordinates()); |
||
153 | $image_filename = "/{$image_filename_prefix}-" . $drawing->getCoordinates(); |
||
154 | $image_suffix = $this->saveImage($drawing, $image_filename); |
||
155 | $image_name = ltrim($this->relative_path, '/') . "{$image_filename}.{$image_suffix}"; |
||
156 | if(isset($this->title_fields[$column])) { |
||
157 | $result[$row-($this->titleFieldsRow+1)][$this->title_fields[$column]] = $image_name; |
||
158 | } |
||
159 | } |
||
160 | return $result; |
||
161 | } |
||
162 | |||
163 | /** |
||
164 | * getTitle |
||
165 | * @return mixed |
||
166 | * @author: Tinymeng <[email protected]> |
||
167 | * @time: 2022/2/22 11:30 |
||
168 | */ |
||
169 | public function getTitle(){ |
||
170 | return $this->title; |
||
171 | } |
||
172 | |||
173 | /** |
||
174 | * getTitleFields |
||
175 | * @return array |
||
176 | * @author: Tinymeng <[email protected]> |
||
177 | * @time: 2022/2/22 11:30 |
||
178 | */ |
||
179 | public function getTitleFields(){ |
||
180 | $title = $this->getTitle(); |
||
181 | |||
182 | $row = $this->titleFieldsRow; |
||
183 | $titleDataArr = []; |
||
184 | |||
185 | foreach ($this->cellName as $column){ |
||
186 | $value = trim($this->workSheet->getCell($column.$row)->getValue()); |
||
187 | if(!empty($value)){ |
||
188 | $titleDataArr[$value] = $column; |
||
189 | } |
||
190 | } |
||
191 | $title_fields = []; |
||
192 | foreach ($title as $key=>$value) { |
||
193 | if(isset($titleDataArr[$key])){ |
||
194 | $title_fields[$titleDataArr[$key]] = $value; |
||
195 | } |
||
196 | } |
||
197 | $this->title_fields = $title_fields; |
||
198 | return $this; |
||
199 | } |
||
200 | |||
201 | /** |
||
202 | * 保存图片到文件相对路径 |
||
203 | * @param Drawing $drawing |
||
204 | * @param $image_filename |
||
205 | * @return string |
||
206 | * @throws Exception |
||
207 | */ |
||
208 | protected function saveImage(Drawing $drawing, $image_filename) |
||
209 | { |
||
210 | FileTool::mkdir($this->image_path); |
||
211 | |||
212 | // 获取文件的真实MIME类型 |
||
213 | $finfo = new \finfo(FILEINFO_MIME_TYPE); |
||
214 | $mime_type = $finfo->file($drawing->getPath()); |
||
215 | |||
216 | // 根据MIME类型确定真实的图片格式 |
||
217 | switch ($mime_type) { |
||
218 | case 'image/jpg': |
||
219 | case 'image/jpeg': |
||
220 | $real_extension = 'jpg'; |
||
221 | $image_filename .= '.'.$real_extension; |
||
222 | $source = imagecreatefromjpeg($drawing->getPath()); |
||
223 | imagejpeg($source, $this->image_path . $image_filename); |
||
224 | break; |
||
225 | case 'image/gif': |
||
226 | $real_extension = 'gif'; |
||
227 | $image_filename .= '.'.$real_extension; |
||
228 | $source = imagecreatefromgif($drawing->getPath()); |
||
229 | imagegif($source, $this->image_path . $image_filename); |
||
230 | break; |
||
231 | case 'image/png': |
||
232 | $real_extension = 'png'; |
||
233 | $image_filename .= '.'.$real_extension; |
||
234 | $source = imagecreatefrompng($drawing->getPath()); |
||
235 | // 保持透明度设置 |
||
236 | imagealphablending($source, false); |
||
237 | imagesavealpha($source, true); |
||
238 | imagepng($source, $this->image_path . $image_filename); |
||
239 | break; |
||
240 | default: |
||
241 | throw new Exception('image format error!'); |
||
242 | } |
||
243 | |||
244 | return $real_extension; |
||
245 | } |
||
246 | } |
||
247 |