Skip to content

Commit aa2d737

Browse files
committed
带图片的导出
1 parent 5b569e1 commit aa2d737

File tree

2 files changed

+37
-4
lines changed

2 files changed

+37
-4
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ return Excel::exportData($list, $header, '测试', 'xlsx', '/www/data/');
5959
// 另外一种导出csv方式
6060
return Excel::exportCsvData($list, $header);
6161
62+
// 带图片的
63+
Excel::exportData($list, $header,date('Y-m-d h:i:s'),'xlsx','',['D','E']);
64+
Excel::exportData($list, $header,date('Y-m-d h:i:s'),'xlsx','',[4,5]);
65+
66+
6267
```
6368

6469
### 导入

src/Excel.php

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use PhpOffice\PhpSpreadsheet\Writer\Xls;
99
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
1010
use PhpOffice\PhpSpreadsheet\Writer\Csv;
11+
use PhpOffice\PhpSpreadsheet\Worksheet\Drawing;
1112

1213
/**
1314
* 导出导入Excel
@@ -29,7 +30,7 @@ class Excel
2930
* @throws \PhpOffice\PhpSpreadsheet\Exception
3031
* @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
3132
*/
32-
public static function exportData($list = [], $header = [], $filename = '', $suffix = 'xlsx', $path = '')
33+
public static function exportData($list = [], $header = [], $filename = '', $suffix = 'xlsx', $path = '', $image = [])
3334
{
3435
if (!is_array ($list) || !is_array ($header)) {
3536
return false;
@@ -64,7 +65,31 @@ public static function exportData($list = [], $header = [], $filename = '', $suf
6465
// 解析字段
6566
$realData = self::formatting($header[$key], trim(self::formattingField($row, $value[1])), $row);
6667
// 写入excel
67-
$sheet->setCellValue(Coordinate::stringFromColumnIndex($span) . $column, $realData);
68+
$rowR = Coordinate::stringFromColumnIndex($span);
69+
$sheet->getColumnDimension($rowR)->setWidth(20);
70+
if(in_array($span,$image)&&file_exists($realData) || in_array($rowR,$image)&&file_exists($realData)){ // 如果这一列应该是图片
71+
$drawing = new Drawing();
72+
$drawing->setName('image');
73+
$drawing->setDescription('image');
74+
try{
75+
$drawing->setPath($realData);
76+
}catch(\Exception $e){
77+
echo $e->getMessage();
78+
echo '<br>可能是图片丢失了或者无权限';
79+
die;
80+
}
81+
82+
$drawing->setWidth(80);
83+
$drawing->setHeight(80);
84+
$drawing->setCoordinates($rowR . $column);//A1
85+
$drawing->setOffsetX(12);
86+
$drawing->setOffsetY(12);
87+
$drawing->setWorksheet($spreadsheet->getActiveSheet());
88+
}else{
89+
$sheet->setCellValue($rowR . $column, $realData);
90+
}
91+
92+
6893
$span++;
6994
}
7095

@@ -129,7 +154,6 @@ public static function exportData($list = [], $header = [], $filename = '', $suf
129154

130155
break;
131156
}
132-
133157
return true;
134158
}
135159

@@ -307,7 +331,11 @@ protected static function formattingField($row, $field)
307331
{
308332
$newField = explode('.', $field);
309333
if (count($newField) == 1) {
310-
return $row[$field];
334+
if(isset($row[$field])){
335+
return $row[$field];
336+
}else{
337+
return false;
338+
}
311339
}
312340

313341
foreach ($newField as $item) {

0 commit comments

Comments
 (0)