Skip to content

Commit 61ab86f

Browse files
authored
Update Excel.php
public static function beforeSheet(BeforeSheet $event) { Excel::handleCalculateSheet($event); } public static function afterSheet(AfterSheet $event) { $result = [ ['字段说明', null, null, null], ['字段名', '示例', '格式说明', '备注'], ['*初筛条码号/就诊卡号', Excel::valueToCellString("20220203001ABC"), '文字', "患者的就诊卡号"], ['母亲姓名', '张三', '文字', "母亲姓名与受检者姓名需要至少填写一个"], ['*受检者姓名', '张二三', '文字', "母亲姓名与受检者姓名需要至少填写一个"], ['受检者性别', '男', '文字', "默认 未知 可选值: 未知/男/女"], ['出生日期', '2022/03/04', '年/月/日', "格式需要注意一下。年是 4位的,使用分隔符 / 区分年月日"], ['采样日期', '2022/03/04', '年/月/日', "格式需要注意一下。年是 4位的,使用分隔符 / 区分年月日"], ['*实验室编号/样本编号', Excel::valueToCellString('20220203002'), '文字', "可选值: 未知/男/女,默认 未知"], ['样本类型', '未知', '文字', "默认 未知 可选值: %s" . collect(AppUtility::getDictionaryData('sample_type'))->pluck('name')->join('/')], ['接样日期', '2022/03/04', '年/月/日', "格式需要注意一下。年是 4位的,使用分隔符 / 区分年月日"], ['检测日期', '2022/03/04', '年/月/日', "格式需要注意一下。年是 4位的,使用分隔符 / 区分年月日"], ['报告日期', '2022/03/04', '年/月/日', "格式需要注意一下。年是 4位的,使用分隔符 / 区分年月日"], ['采血单位', '国科大医院西院区检验科', '文字', '科室名称'], ['*检测项目', '合法的检测项目名称', '文字', null], ['*移动电话', '受检者手机号', '11 位手机号', null], ['结果', '通过', '文字', '未知/通过/不通过/阳性/阴性'], ['异常情况', '异常情况说明', '文字', null], ['医院', '国科大医院', '文字', null], ]; // 从数组设置数据数据 Excel::loadDataFromArray($event, $result, 'A', 1); // 带 * 单元格红色标记 Excel::handleRequireCellTextColorForRed($event); // 合并单元格 Excel::mergeCells($event, 'A1:D1'); // 设置表头加粗 Excel::setTitleStyle($event, "A1"); } public static function afterSheet(AfterSheet $event) { // 表头加租居中 Excel::setTitleStyle($event, "A1:Q1"); // 带 * 单元格红色标记 Excel::handleRequireCellTextColorForRed($event); }
1 parent c180f9f commit 61ab86f

File tree

1 file changed

+88
-6
lines changed

1 file changed

+88
-6
lines changed

src/Utils/Excel.php

Lines changed: 88 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
use Carbon\Carbon;
66
use Maatwebsite\Excel\Events\Event;
7+
use PhpOffice\PhpSpreadsheet\Style\Color;
8+
use PhpOffice\PhpSpreadsheet\Cell\Hyperlink;
79
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
810

911
class Excel
@@ -126,6 +128,7 @@ public static function datetimeFromCell(mixed $datetime = null, $format = 'Y-m-d
126128

127129
/**
128130
* 带 * 单元格红色标记
131+
* 带 :// 添加超链接
129132
*
130133
* call in:
131134
* public static function afterSheet(AfterSheet $event)
@@ -144,11 +147,11 @@ public static function handleRequireCellTextColorForRed(Event $event)
144147

145148
foreach (range(0, $maxRow) as $row) {
146149
foreach (range(0, $maxCol) as $col) {
147-
$colName = chr($col + 65);
148-
$cell = "{$colName}{$row}";
150+
$columnLetter = chr($col + 65);
151+
$cell = "{$columnLetter}{$row}";
149152

150153
// 设置列宽 autoSize
151-
$sheet->getColumnDimension($colName)->setAutoSize(true);
154+
$sheet->getColumnDimension($columnLetter)->setAutoSize(true);
152155

153156
try {
154157
$calcValue = $sheet->getCell($cell)->getCalculatedValue();
@@ -169,8 +172,13 @@ public static function handleRequireCellTextColorForRed(Event $event)
169172
$newValue = $sheet->getCell($cell)->getValue();
170173

171174
if (str_contains($newValue ?? '', '*')) {
172-
$sheet->getStyle($cell)->getFont()->getColor()->setARGB(\PhpOffice\PhpSpreadsheet\Style\Color::COLOR_RED);
175+
$sheet->getStyle($cell)->getFont()->getColor()->setARGB(Color::COLOR_RED);
176+
}
177+
178+
if (str_contains($newValue ?? '', '://')) {
179+
Excel::cellAddHyperLink($event, $cell);
173180
}
181+
174182
$sheet->getCell($cell)->setValue($newValue);
175183
}
176184
}
@@ -221,6 +229,80 @@ public static function loadDataFromArray(Event $event, array $data = [], string
221229
}
222230
}
223231

232+
/**
233+
* 给指定列添加超链接
234+
*
235+
* call in:
236+
* public static function afterSheet(AfterSheet $event)
237+
*
238+
* @param Event $event
239+
* @param string $columnLetter
240+
* @param string $tooltip
241+
* @return void
242+
*/
243+
public static function hyper(Event $event, string $columnLetter, $tooltip = "查看")
244+
{
245+
$sheet = Excel::getSheet($event);
246+
247+
foreach ($sheet->getColumnIterator($columnLetter) as $row) {
248+
foreach ($row->getCellIterator() as $cell) {
249+
$value = $cell->getValue();
250+
251+
if (str_contains($value, '://')) {
252+
$cell->setHyperlink(new Hyperlink($value, $tooltip));
253+
254+
Excel::cellAddColor($event, $cell);
255+
}
256+
}
257+
}
258+
}
259+
260+
/**
261+
* 给指定单元格添加超链接
262+
*
263+
* call in:
264+
* public static function afterSheet(AfterSheet $event)
265+
*
266+
* @param Event $event
267+
* @param string $cell
268+
* @param string $tooltip
269+
* @return void
270+
*/
271+
public static function cellAddHyperLink(Event $event, string $cell, $tooltip = "查看")
272+
{
273+
$sheet = Excel::getSheet($event);
274+
275+
$sheetCell = $sheet->getCell($cell);
276+
277+
$value = $sheetCell->getValue();
278+
279+
if (str_contains($value, '://')) {
280+
$sheetCell->setHyperlink(new Hyperlink($value, $tooltip));
281+
282+
Excel::cellAddColor($event, $sheetCell->getCoordinate(), Color::COLOR_BLUE);
283+
}
284+
}
285+
286+
/**
287+
* 给指定单元格添加颜色
288+
*
289+
* call in:
290+
* public static function afterSheet(AfterSheet $event)
291+
*
292+
* @param Event $event
293+
* @param string $cell
294+
* @param [type] $color
295+
* @return void
296+
*/
297+
public static function cellAddColor(Event $event, string $cell, $color = Color::COLOR_BLACK)
298+
{
299+
$sheet = Excel::getSheet($event);
300+
301+
$sheetCell = $sheet->getCell($cell);
302+
303+
$sheetCell->getStyle()->getFont()->getColor()->setARGB($color);
304+
}
305+
224306
/**
225307
* 合并单元格
226308
*
@@ -286,7 +368,7 @@ public static function setCellStyle(Event $event, string $cellOrRange, array $st
286368
}
287369

288370
/**
289-
*
371+
* 单元格数字转文字显示
290372
*
291373
* call in:
292374
* public static function afterSheet(AfterSheet $event)
@@ -297,7 +379,7 @@ public static function setCellStyle(Event $event, string $cellOrRange, array $st
297379
*
298380
* @param string $format
299381
* @param string|array ...$value
300-
* @return void
382+
* @return string
301383
*/
302384
public static function valueToCellString($format = '="%s"', ...$value): string
303385
{

0 commit comments

Comments
 (0)