PHP导出Excel带图片
如果想要在导出的表格单元格加入图片,需要这样做。
首先安装扩展库:phpoffice/phpexcel
① 使用PhpSpreadsheet导出
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Worksheet\Drawing;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
require_once 'vendor/autoload.php';
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
//将图片绘制到excel中
$drawing = new Drawing();
$drawing->setPath('图片位置');
$drawing->setHeight(50);//单元格高度
$drawing->setCoordinates('A1');
$drawing->setWorksheet($sheet);
$writer = new Xlsx($spreadsheet);
$writer->save('ggdoc.xlsx');
② 使用PHPExcel_Worksheet导出
require_once 'vendor/autoload.php';
$spreadsheet = new \PHPExcel();
$sheet = $spreadsheet->getActiveSheet();
//将图片绘制到excel中
$drawing = new \PHPExcel_Worksheet_Drawing();
$drawing->setPath('图片位置');
$drawing->setResizeProportional(false);
$drawing->setHeight(300);
$drawing->setWidth(200);
$drawing->setOffsetX(20);
$drawing->setOffsetY(20);
$drawing->setCoordinates('A1');
$drawing->setWorksheet($sheet);
$writer = \PHPExcel_IOFactory::createWriter($spreadsheet, 'Excel2007');
$writer->save('ggdoc.xlsx');
代码仅供参考。