插件窝 干货文章 php使用phpoffice/phpspreadsheet导出图片实例

php使用phpoffice/phpspreadsheet导出图片实例

Drawing spreadsheet PhpSpreadsheet 图片 435    来源:    2024-10-17

一:方法介绍

导出插入图片主要使用\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文件

三:参考

基础操作参考:https://www.jb51.net/program/304059ta3.htm

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

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

以上就是php使用phpoffice/phpspreadsheet导出图片实例的详细内容,更多关于phpoffice/phpspreadsheet导出图片的资料请关注插件窝其它相关文章!