|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @name: CellValueHandler |
|
4
|
|
|
* @author: JiaMeng <[email protected]> |
|
5
|
|
|
* @file: CellValueHandler.php |
|
6
|
|
|
* @Date: 2025/01/XX |
|
7
|
|
|
*/ |
|
8
|
|
|
namespace tinymeng\spreadsheet\Excel\Handler; |
|
9
|
|
|
|
|
10
|
|
|
use PhpOffice\PhpSpreadsheet\Cell\DataType; |
|
11
|
|
|
use PhpOffice\PhpSpreadsheet\Worksheet\Drawing; |
|
12
|
|
|
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet; |
|
13
|
|
|
use tinymeng\spreadsheet\Util\WorkSheetHelper; |
|
14
|
|
|
|
|
15
|
|
|
class CellValueHandler |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* 设置单元格值 |
|
19
|
|
|
* @param Worksheet $worksheet |
|
20
|
|
|
* @param array $val 数据行 |
|
21
|
|
|
* @param array $fields 字段列表 |
|
22
|
|
|
* @param int $row 当前行 |
|
23
|
|
|
* @param int $titleRow 标题行数 |
|
24
|
|
|
* @param int|null $height 行高 |
|
25
|
|
|
* @param bool $autoDataType 是否自动数据类型 |
|
26
|
|
|
* @param bool $format 是否格式化内容 |
|
27
|
|
|
* @param string $formatDate 日期格式 |
|
28
|
|
|
* @return int 返回更新后的行号 |
|
29
|
|
|
*/ |
|
30
|
|
|
public static function setCellValue( |
|
31
|
|
|
Worksheet $worksheet, |
|
32
|
|
|
array $val, |
|
33
|
|
|
array $fields, |
|
34
|
|
|
int $row, |
|
35
|
|
|
int $titleRow, |
|
36
|
|
|
?int $height = null, |
|
37
|
|
|
bool $autoDataType = false, |
|
38
|
|
|
bool $format = true, |
|
39
|
|
|
string $formatDate = 'Y-m-d H:i:s' |
|
40
|
|
|
): int { |
|
41
|
|
|
// 设置单元格行高 |
|
42
|
|
|
if (!empty($height)) { |
|
43
|
|
|
$worksheet->getRowDimension($row)->setRowHeight($height); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
$_lie = 0; |
|
47
|
|
|
foreach ($fields as $v) { |
|
48
|
|
|
$rowName = WorkSheetHelper::cellName($_lie); |
|
49
|
|
|
|
|
50
|
|
|
// 处理嵌套字段(如 'user.name') |
|
51
|
|
|
if (strpos($v, '.') !== false) { |
|
52
|
|
|
$v = explode('.', $v); |
|
53
|
|
|
$content = $val; |
|
54
|
|
|
for ($i = 0; $i < count($v); $i++) { |
|
|
|
|
|
|
55
|
|
|
$content = $content[$v[$i]] ?? ''; |
|
56
|
|
|
} |
|
57
|
|
|
} elseif ($v == '_id') { |
|
58
|
|
|
$content = $row - $titleRow; // 自增序号列 |
|
59
|
|
|
} else { |
|
60
|
|
|
$content = ($val[$v] ?? ''); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
// 处理图片类型 |
|
64
|
|
|
if (is_array($content) && isset($content['type']) && isset($content['content'])) { |
|
65
|
|
|
if ($content['type'] == 'image') { |
|
66
|
|
|
self::setImage($worksheet, $content, $rowName, $row); |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
// 处理公式类型 |
|
70
|
|
|
elseif (is_array($content) && isset($content['formula'])) { |
|
71
|
|
|
$worksheet->setCellValueExplicit( |
|
72
|
|
|
$rowName . $row, |
|
73
|
|
|
$content['formula'], |
|
74
|
|
|
DataType::TYPE_FORMULA |
|
75
|
|
|
); |
|
76
|
|
|
} |
|
77
|
|
|
// 处理普通值 |
|
78
|
|
|
else { |
|
79
|
|
|
$content = WorkSheetHelper::formatValue($content, $format, $formatDate); // 格式化数据 |
|
80
|
|
|
if (is_numeric($content)) { |
|
81
|
|
|
if ($autoDataType && strlen($content) < 11) { |
|
82
|
|
|
$worksheet->setCellValueExplicit($rowName . $row, $content, DataType::TYPE_NUMERIC); |
|
83
|
|
|
} else { |
|
84
|
|
|
$worksheet->setCellValueExplicit($rowName . $row, $content, DataType::TYPE_STRING2); |
|
85
|
|
|
} |
|
86
|
|
|
} else { |
|
87
|
|
|
$worksheet->setCellValueExplicit($rowName . $row, $content, DataType::TYPE_STRING2); |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
$_lie++; |
|
91
|
|
|
} |
|
92
|
|
|
$row++; |
|
93
|
|
|
|
|
94
|
|
|
return $row; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* 设置图片到单元格 |
|
99
|
|
|
* @param Worksheet $worksheet |
|
100
|
|
|
* @param array $imageConfig 图片配置 ['type'=>'image', 'content'=>'路径', 'height'=>100, 'width'=>100, 'offsetX'=>0, 'offsetY'=>0] |
|
101
|
|
|
* @param string $rowName 列字母 |
|
102
|
|
|
* @param int $row 行号 |
|
103
|
|
|
*/ |
|
104
|
|
|
private static function setImage( |
|
105
|
|
|
Worksheet $worksheet, |
|
106
|
|
|
array $imageConfig, |
|
107
|
|
|
string $rowName, |
|
108
|
|
|
int $row |
|
109
|
|
|
) { |
|
110
|
|
|
$path = WorkSheetHelper::verifyFile($imageConfig['content']); |
|
111
|
|
|
$drawing = new Drawing(); |
|
112
|
|
|
$drawing->setPath($path); |
|
113
|
|
|
|
|
114
|
|
|
if (!empty($imageConfig['height'])) { |
|
115
|
|
|
$drawing->setHeight($imageConfig['height']); |
|
116
|
|
|
} |
|
117
|
|
|
if (!empty($imageConfig['width'])) { |
|
118
|
|
|
$drawing->setWidth($imageConfig['width']); // 只设置高,宽会自适应,如果设置宽后,高则失效 |
|
119
|
|
|
} |
|
120
|
|
|
if (!empty($imageConfig['offsetX'])) { |
|
121
|
|
|
$drawing->setOffsetX($imageConfig['offsetX']); // 设置X方向偏移量 |
|
122
|
|
|
} |
|
123
|
|
|
if (!empty($imageConfig['offsetY'])) { |
|
124
|
|
|
$drawing->setOffsetY($imageConfig['offsetY']); // 设置Y方向偏移量 |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
$drawing->setCoordinates($rowName . $row); |
|
128
|
|
|
$drawing->setWorksheet($worksheet); |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
|
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: