我想将图像放在我的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文件中没有图像,请谁想要帮助我。
答案 0 :(得分:2)
我正在使用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');
答案 1 :(得分:1)
我为您找到了解决方案。但不幸的是,我找不到一种使用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');
工作正常,请尽情享受:)
答案 2 :(得分:1)
public struct Result<TResult>
{
public static Result<TResult> Ok(TResult data) => new Result<TResult>(data, true);
public static Result<TResult> Error() => new Result<TResult>(default(TResult), false);
private Result(TResult data, bool success)
{
Data = data;
Success = success;
}
public bool Success { get; }
public TResult Data { get; }
}
public static class TaskExt
{
public static async Task<Result<T>> AwaitSafe<T, TException>(this Task<T> task, Action<TException> handle)
where TException : Exception
{
var result = Result<T>.Error();
try
{
result = Result<T>.Ok(await task);
}
catch (TException ex)
{
handle.Invoke(ex);
}
return result;
}
}