1

基础操作参考:https://blog.csdn.net/huaweichenai/article/details/95994006

文档地址:https://phpspreadsheet.readthedocs.io/

github地址:https://github.com/PHPOffice/PhpSpreadsheet

导出插入图片主要使用\PhpOffice\PhpSpreadsheet\Worksheet\Drawing实现

一:方法介绍

$drawing = new \PhpOffice\PhpSpreadsheet\Worksheet\Drawing();
$drawing->setName('Logo');//设置图片名称
$drawing->setDescription('Logo');//设备图片描述
$drawing->setPath('./images/officelogo.jpg');//设置图片地址
$drawing->setHeight(36);//设置图片高度
$drawing->setCoordinates('A1');//设置图片绘制到指定单元格
$drawing->setWorksheet($spreadsheet->getActiveSheet());//将图片绘制到工作表

二:实现实例

$spreadsheet = new Spreadsheet();//创建一个新的excel文档
$sheet = $spreadsheet->getActiveSheet();//获取当前操作sheet的对象
//将图片绘制到excel中
$drawing = new \PhpOffice\PhpSpreadsheet\Worksheet\Drawing();
$drawing->setPath($img);
$drawing->setHeight(50);
$drawing->setCoordinates('A1');
$drawing->setWorksheet($sheet);
$writer = new Xlsx($spreadsheet);
$writer->save('xxx.xlsx');//生成excel文件


//将多个图片绘制到excel中
$spreadsheet = new Spreadsheet();//创建一个新的excel文档
$sheet = $spreadsheet->getActiveSheet();//获取当前操作sheet的对象
//将图片绘制到excel中
foreach ($imgs as $img) {
    $drawing = new \PhpOffice\PhpSpreadsheet\Worksheet\Drawing();
    $drawing->setPath($img);
    $drawing->setHeight(50);
    $drawing->setCoordinates('A1');
    $drawing->setWorksheet($sheet);
}
$writer = new Xlsx($spreadsheet);
$writer->save('xxx.xlsx');//生成excel文件

huaweichenai
635 声望114 粉丝