php 在phpexcel中将图片添加到Excel中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27716946/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:29:25  来源:igfitidea点击:

Adding Image to the Excel in phpexcel in php

phpphpexcelphpexcelreader

提问by AngularAngularAngular

I am taking the example from phpexcel

我正在从phpexcel 中举个例子

I just tried with passing value in GET Method, I am done with that.

我只是尝试在 GET 方法中传递值,我已经完成了。

Now i am trying to add image in the a3 coloumn.

现在我正在尝试在 a3 列中添加图像。

Reference Code :

参考代码 :

<?php
$value = $_GET['value'];
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
date_default_timezone_set('Europe/London');

if (PHP_SAPI == 'cli')
    die('This example should only be run from a Web Browser');

require_once dirname(__FILE__) . '/Classes/PHPExcel.php';


$objPHPExcel = new PHPExcel();


$objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
                             ->setLastModifiedBy("Maarten Balliauw")
                             ->setTitle("Office 2007 XLSX Test Document")
                             ->setSubject("Office 2007 XLSX Test Document")
                             ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
                             ->setKeywords("office 2007 openxml php")
                             ->setCategory("Test result file");


$objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A1', $value)
            ->setCellValue('B2', 'world!')
            ->setCellValue('C1', 'Hello')
            ->setCellValue('D2', 'world!');


$objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A4', 'Miscellaneous glyphs')
            ->setCellValue('A5', 'éàèùaê?????ü???ü?');

$objPHPExcel->getActiveSheet()->setTitle('Simple');


$objPHPExcel->setActiveSheetIndex(0);
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="01simple.xls"');
header('Cache-Control: max-age=0');
header('Cache-Control: max-age=1');

header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
header ('Pragma: public'); // HTTP/1.0

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
exit;
?>

Example Code for inserting image :

插入图像的示例代码:

$gdImage = imagecreatefromjpeg('images/officelogo.jpg');
// Add a drawing to the worksheetecho date('H:i:s') . " Add a drawing to the worksheet\n";
$objDrawing = new PHPExcel_Worksheet_MemoryDrawing();
$objDrawing->setName('Sample image');$objDrawing->setDescription('Sample image');
$objDrawing->setImageResource($gdImage);
$objDrawing->setRenderingFunction(PHPExcel_Worksheet_MemoryDrawing::RENDERING_JPEG);
$objDrawing->setMimeType(PHPExcel_Worksheet_MemoryDrawing::MIMETYPE_DEFAULT);
$objDrawing->setHeight(150);
$objDrawing->setWorksheet($objPHPExcel->getActiveSheet());
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');$objWriter->save(str_replace('.php', '.xlsx', __FILE__));

But i don't understanding how to insert the jpg in the a3 coloumn or any other coloumn in the excel file that i import.

但我不明白如何在 a3 列或我导入的 excel 文件中的任何其他列中插入 jpg。

How can i do this ?

我怎样才能做到这一点 ?

采纳答案by Mark Baker

Specifying coordinates for the image might help, as per the examples and the documentation

根据示例和文档,为图像指定坐标可能会有所帮助

$objDrawing->setCoordinates('A3');

Note that an image isn't ina cell/column/row, but overlaidover the main sheet at the same position as that cell/column/row

请注意,图像不是一个小区/列/行,但覆盖在主片在相同位置作为该小区/列/行

回答by Yuva Kumar

Read my article,

阅读我的文章,

http://www.7codes.info/post/8/export-excel-files-with-images-using-php-excel-library

http://www.7codes.info/post/8/export-excel-files-with-images-using-php-excel-library

$objDrawing = new PHPExcel_Worksheet_Drawing();    //create object for Worksheet drawing
$objDrawing->setName('Customer Signature');        //set name to image
$objDrawing->setDescription('Customer Signature'); //set description to image
$signature = $reportdetails[$rowCount][$value];    //Path to signature .jpg file
$objDrawing->setPath($signature);
$objDrawing->setOffsetX(25);                       //setOffsetX works properly
$objDrawing->setOffsetY(10);                       //setOffsetY works properly
$objDrawing->setCoordinates($column.$cell);        //set image to cell
$objDrawing->setWidth(32);                 //set width, height
$objDrawing->setHeight(32);  

$objDrawing->setWorksheet($objPHPExcel->getActiveSheet());  //save

回答by matinict

I solve logo or images insert/showing problem use below code:

我使用下面的代码解决了徽标或图像插入/显示问题:

$objDrawing = new PHPExcel_Worksheet_Drawing();
$objDrawing->setName('test_img');
$objDrawing->setDescription('test_img');
$objDrawing->setPath('../images/logo.png');
$objDrawing->setCoordinates('A1');                      
//setOffsetX works properly
$objDrawing->setOffsetX(5); 
$objDrawing->setOffsetY(5);                
//set width, height
$objDrawing->setWidth(100); 
$objDrawing->setHeight(35); 
$objDrawing->setWorksheet($objPHPExcel->getActiveSheet());