Skip to content

Commit 08eaae3

Browse files
authored
Merge pull request #3 from shoakepei/master
导出包括图片 本地图片 和 来自网络的图片都可以
2 parents 5b569e1 + 981e281 commit 08eaae3

File tree

2 files changed

+77
-4
lines changed

2 files changed

+77
-4
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ $header = [
3030
return $model['sex'] == 1 ? '男' : '女';
3131
}],
3232
['创建时间', 'created_at', 'date', 'Y-m-d'],
33+
['图片', 'image', 'text'],// 本地图片 ./images/765456898612.jpg
3334
];
3435
3536
$list = [
@@ -59,6 +60,11 @@ return Excel::exportData($list, $header, '测试', 'xlsx', '/www/data/');
5960
// 另外一种导出csv方式
6061
return Excel::exportCsvData($list, $header);
6162
63+
// 带图片的
64+
Excel::exportData($list, $header,date('Y-m-d h:i:s'),'xlsx','',['D','E']);
65+
Excel::exportData($list, $header,date('Y-m-d h:i:s'),'xlsx','',[4,5]);
66+
67+
6268
```
6369

6470
### 导入

src/Excel.php

Lines changed: 71 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,58 @@ 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) || in_array($rowR,$image) ){ // 如果这一列应该是图片
71+
if(file_exists($realData)){ // 本地文件
72+
$drawing = new Drawing();
73+
$drawing->setName('image');
74+
$drawing->setDescription('image');
75+
try{
76+
$drawing->setPath($realData);
77+
}catch(\Exception $e){
78+
echo $e->getMessage();
79+
echo '<br>可能是图片丢失了或者无权限';
80+
die;
81+
}
82+
83+
$drawing->setWidth(80);
84+
$drawing->setHeight(80);
85+
$drawing->setCoordinates($rowR . $column);//A1
86+
$drawing->setOffsetX(12);
87+
$drawing->setOffsetY(12);
88+
$drawing->setWorksheet($spreadsheet->getActiveSheet());
89+
}else{ // 可能是 网络文件
90+
$img = self::curlGet($realData);
91+
$file_info = pathinfo($realData);
92+
$extension = $file_info['extension'];// 文件后缀
93+
$dir = '.' . DIRECTORY_SEPARATOR . 'execlImg'. DIRECTORY_SEPARATOR . \date('Y-m-d'). DIRECTORY_SEPARATOR;// 文件夹名
94+
$basename = time(). mt_rand(1000,9999).'.'.$extension;// 文件名
95+
is_dir($dir) OR mkdir($dir, 0777, true); //进行检测文件夹是否存在
96+
file_put_contents($dir.$basename , $img);
97+
$drawing = new Drawing();
98+
$drawing->setName('image');
99+
$drawing->setDescription('image');
100+
try{
101+
$drawing->setPath($dir.$basename);
102+
}catch(\Exception $e){
103+
echo $e->getMessage();
104+
echo '<br>可能是图片丢失了或者无权限';
105+
die;
106+
}
107+
108+
$drawing->setWidth(80);
109+
$drawing->setHeight(80);
110+
$drawing->setCoordinates($rowR . $column);//A1
111+
$drawing->setOffsetX(12);
112+
$drawing->setOffsetY(12);
113+
$drawing->setWorksheet($spreadsheet->getActiveSheet());
114+
}
115+
}else{
116+
$sheet->setCellValue($rowR . $column, $realData);
117+
}
118+
119+
68120
$span++;
69121
}
70122

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

130182
break;
131183
}
132-
133184
return true;
134185
}
135186

@@ -307,7 +358,11 @@ protected static function formattingField($row, $field)
307358
{
308359
$newField = explode('.', $field);
309360
if (count($newField) == 1) {
310-
return $row[$field];
361+
if(isset($row[$field])){
362+
return $row[$field];
363+
}else{
364+
return false;
365+
}
311366
}
312367

313368
foreach ($newField as $item) {
@@ -320,4 +375,16 @@ protected static function formattingField($row, $field)
320375

321376
return is_array($row) ? false : $row;
322377
}
378+
379+
public static function curlGet($url)
380+
{
381+
$ch = \curl_init();
382+
\curl_setopt($ch, CURLOPT_URL, $url);
383+
\curl_setopt($ch, CURLOPT_HEADER, 0);
384+
\curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
385+
\curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 这个是重点 请求https。
386+
$data = \curl_exec($ch);
387+
\curl_close($ch);
388+
return $data;
389+
}
323390
}

0 commit comments

Comments
 (0)