PHPSpreadsheet - 如何将 link 中的图像放入我的 excel 文件中?

PHPSpreadsheet - How do i place a image from link into my excel file?

我想将我的 excel 文件中的图像放在单元格 a1 中。

我从 phpspreadsheet 网站尝试了不同的方法,但都没有成功。 就像他们的这个不起作用的例子一样。:

$drawing = new \PhpOffice\PhpSpreadsheet\Worksheet\Drawing();
$drawing->setName('Paid');
$drawing->setDescription('Paid');
$drawing->setPath('https://pngimage.net/wp-content/uploads/2018/06/rfhnbyrb-png-6.png');
$drawing->setCoordinates('B15');
$drawing->setOffsetX(110);
$drawing->setRotation(25);
$drawing->getShadow()->setVisible(true);
$drawing->getShadow()->setDirection(45);

如果我创建一个黑色条,里面有文本,它可以使用此代码。:

$gdImage = @imagecreatetruecolor(2000, 20) or die('Cannot Initialize new GD image stream');
$textColor = imagecolorallocate($gdImage, 255, 255, 5);
imagestring($gdImage, 1, 5, 5, date("F Y"), $textColor);

//  Add the In-Memory image to a worksheet
$drawing = new \PhpOffice\PhpSpreadsheet\Worksheet\MemoryDrawing();
$drawing->setName('In-Memory image 1');
$drawing->setDescription('In-Memory image 1');
$drawing->setCoordinates('A1');
$drawing->setImageResource($gdImage);
$drawing->setRenderingFunction(\PhpOffice\PhpSpreadsheet\Worksheet\MemoryDrawing::RENDERING_JPEG);
$drawing->setMimeType(\PhpOffice\PhpSpreadsheet\Worksheet\MemoryDrawing::MIMETYPE_DEFAULT);
$drawing->setHeight(36);
$drawing->setWorksheet($spreadsheet->getActiveSheet());

我试过我的 excel 文件中没有图像,请谁愿意帮助我。

我为您找到了解决方案。但不幸的是,我找不到使用 url 的方法,只能在本地使用图像。看看。

require 'vendor/autoload.php';

use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;

$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();

$drawing = new \PhpOffice\PhpSpreadsheet\Worksheet\Drawing();
$drawing->setName('Paid');
$drawing->setDescription('Paid');
$drawing->setPath('images/paid.png'); // put your path and image here
$drawing->setCoordinates('B15');
$drawing->setOffsetX(110);
$drawing->setRotation(25);
$drawing->getShadow()->setVisible(true);
$drawing->getShadow()->setDirection(45);
$drawing->setWorksheet($spreadsheet->getActiveSheet());

$writer = new Xlsx($spreadsheet);
$writer->save('image.xlsx');

它工作得很好,享受:)

$drawing = new \PhpOffice\PhpSpreadsheet\Worksheet\MemoryDrawing();
                    $drawing->setImageResource(imagecreatefrompng('https://pngimage.net/wp-content/uploads/2018/06/rfhnbyrb-png-6.png');
                    $drawing->setCoordinates($cell->getCoordinate());
                    $drawing->setWidthAndHeight(50, 50);
                    $drawing->setRenderingFunction(\PhpOffice\PhpSpreadsheet\Worksheet\MemoryDrawing::RENDERING_JPEG);
                    $drawing->setMimeType(\PhpOffice\PhpSpreadsheet\Worksheet\MemoryDrawing::MIMETYPE_DEFAULT);
                    $drawing->setOffsetX(110);
                    $drawing->setRotation(25);
                    $drawing->getShadow()->setVisible(true);
                    $drawing->getShadow()->setDirection(45);
                    $drawing->setWorksheet($csv->getActiveSheet());

我在 PHP 7.2 中使用带有最新 phpspreedsheet 版本的 phpspreedsheet。

<?php

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

require 'vendor/autoload.php';

use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use PhpOffice\PhpSpreadsheet\Worksheet\MemoryDrawing;

$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();

$IMG = 'https://pngimage.net/wp-content/uploads/2018/06/rfhnbyrb-png-6.png';
$row_num = 2;
if (isset($IMG) && !empty($IMG)) {
    $imageType = "png";

    if (strpos($IMG, ".png") === false) {
        $imageType = "jpg";
    }

    $drawing = new MemoryDrawing();
    $sheet->getRowDimension($row_num)->setRowHeight(80);
    $sheet->mergeCells('A'.$row_num.':H'.$row_num);

    $gdImage = ($imageType == 'png') ? imagecreatefrompng($IMG) : imagecreatefromjpeg($IMG);
    $drawing->setName('Company Logo');
    $drawing->setDescription('Company Logo image');
    $drawing->setResizeProportional(false);
    $drawing->setImageResource($gdImage);
    $drawing->setRenderingFunction(\PhpOffice\PhpSpreadsheet\Worksheet\MemoryDrawing::RENDERING_JPEG);
    $drawing->setMimeType(\PhpOffice\PhpSpreadsheet\Worksheet\MemoryDrawing::MIMETYPE_DEFAULT);
    $drawing->setWidth(211);
    $drawing->setHeight(40);
    $drawing->setOffsetX(5);
    $drawing->setOffsetY(30);
    $drawing->setCoordinates('C'.$row_num);
    $drawing->setWorksheet($spreadsheet->getActiveSheet());
    $row_num++;
}

$writer = new Xlsx($spreadsheet);
$writer->save('hello world.xlsx');