使用 PHP 将图像添加到 Excel 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14754817/
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 07:55:31  来源:igfitidea点击:

Add images to a excel file using PHP

phpexcelherokuexport-to-excel

提问by Roch

I'm using the following function to add content to an excel file using PHP :

我正在使用以下函数将内容添加到使用 PHP 的 excel 文件中:

function __writeString($row, $col, $value ) {
        $L = strlen($value);
        $this->file .= pack("ssssss", 0x204, 8 + $L, $row, $col, 0x0, $L);
        $this->file .= $value;
}

I would like to know how I can add images in my cells the same way, by supplying its url as a value for instance.

我想知道如何以相同的方式在我的单元格中添加图像,例如通过提供其 url 作为值。

回答by trigun0x2

Take a look at this: PHPExcel. It will provide you will all the tools you need to read and write Excel from PHP.

看看这个:PHPExcel。它将为您提供从 PHP 读取和编写 Excel 所需的所有工具。

Once you have PHPExcel installed, you can use something like this to insert:

一旦你安装了 PHPExcel,你就可以使用这样的东西来插入:

$objDrawing = new PHPExcel_Worksheet_Drawing();

$objDrawing->setPath('./images/picture.png');

$objDrawing->setCoordinates('A11');

回答by DrupalFever

"Trigun", your suggestion really helped. I was able to download the latest PHPExcel Classes from https://github.com/PHPOffice/PHPExceland was up and running in no time. However, it took some extra time to figure out how to add an image to the excel file. Your explanation didn't help much.

“Trigun”,你的建议真的很有帮助。我能够从https://github.com/PHPOffice/PHPExcel下载最新的 PHPExcel 类,并且立即启动并运行。但是,弄清楚如何将图像添加到 excel 文件需要一些额外的时间。你的解释没有多大帮助。

Here is a complete description of how to do it:

以下是有关如何执行此操作的完整说明:

First, download the library and place it in a logical place on your website:

首先,下载库并将其放置在您网站上的合理位置:

sites/all/libraries/phpexcel/Classes

Now you create your PHP file anywhere in your website that you would like it to be and add the following into the file:

现在,您可以在网站的任意位置创建 PHP 文件,并将以下内容添加到文件中:

1) Allow the error messages to be printed on the screen:

1) 允许在屏幕上打印错误消息:

error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
date_default_timezone_set('Europe/London');

define('EOL',(PHP_SAPI == 'cli') ? PHP_EOL : '<br \>');

2) Include the Excel Classes file:

2) 包含 Excel Classes 文件:

/** Include PHPExcel */
require_once $_SERVER["DOCUMENT_ROOT"] . "/sites/all/libraries/phpexcel/Classes/PHPExcel.php";

3) Create the "PHPExcel" object:

3)创建“PHPExcel”对象:

// Create new PHPExcel object
echo date('H:i:s') , " Create new PHPExcel object" , EOL;
$objPHPExcel = new PHPExcel();

4) Set some Excel metadata such as title and description.

4) 设置一些 Excel 元数据,如标题和描述。

// Set document properties
echo date('H:i:s') , " Set document properties" , EOL;
$objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
->setLastModifiedBy("Maarten Balliauw")
->setTitle("PHPExcel Test Document")
->setSubject("PHPExcel Test Document")
->setDescription("Test document for PHPExcel, generated using PHP classes.")
->setKeywords("office PHPExcel php")
->setCategory("Test result file");

5) Add some data to the "B1" cell:

5)向“B1”单元格添加一些数据:

// Add some data
echo date('H:i:s') , " Add some data" , EOL;
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('B1', 'Hello world!')

6) Create a "drawing" object that we can load our image into. Remember to replace the image URL with a valid image URL in your web server:

6)创建一个“绘图”对象,我们可以将图像加载到其中。请记住将图像 URL 替换为 Web 服务器中的有效图像 URL:

// Add a drawing to the worksheet
echo date('H:i:s') , " Add a drawing to the worksheet" , EOL;
$objDrawing = new PHPExcel_Worksheet_Drawing();
$objDrawing->setName('Thumb');
$objDrawing->setDescription('Thumbnail Image');
$objDrawing->setPath($_SERVER["DOCUMENT_ROOT"] . '/sites/default/files/product-images/10098.jpg');
$objDrawing->setHeight(21);

7) Copy the image into the "A1" cell on our "$objPHPExcel" object.

7) 将图像复制到“$objPHPExcel”对象上的“A1”单元格中。

$objDrawing->setCoordinates('A1');
$objDrawing->setWorksheet($objPHPExcel->getActiveSheet());

8) Saving the "$objPHPExcel" object as an Excel file format.

8) 将“$objPHPExcel”对象保存为 Excel 文件格式。

// Save Excel 95 file
echo date('H:i:s') , " Write to Excel5 format" , EOL;
$callStartTime = microtime(true);

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save(str_replace('.php', '.xls', __FILE__));

9) Print some mildly useful information to the screen:

9) 在屏幕上打印一些稍微有用的信息:

$callEndTime = microtime(true);
$callTime = $callEndTime - $callStartTime;

echo date('H:i:s') , " File written to " , str_replace('.php', '.xls', pathinfo(__FILE__, PATHINFO_BASENAME)) , EOL;
echo 'Call time to write Workbook was ' , sprintf('%.4f',$callTime) , " seconds" , EOL;
// Echo memory usage
echo date('H:i:s') , ' Current memory usage: ' , (memory_get_usage(true) / 1024 / 1024) , " MB" , EOL;


// Echo memory peak usage
echo date('H:i:s') , " Peak memory usage: " , (memory_get_peak_usage(true) / 1024 / 1024) , " MB" , EOL;

// Echo done
echo date('H:i:s') , " Done writing files" , EOL;
echo 'Files have been created in ' , getcwd() , EOL;

This is the whole thing!

这就是整件事!

回答by Ly Thanh Ngo

Once you have PHPExcel installed. Then insert code: The image I used here is relative. Path: './images/logo.jpg'

一旦你安装了 PHPExcel。然后插入代码:我这里使用的图像是相对的。路径:'./images/logo.jpg'

$objDrawing = new PHPExcel_Worksheet_Drawing();
$objDrawing->setPath('./images/logo.jpg');
$objDrawing->setCoordinates('A1');
$objDrawing->setWorksheet($objPHPExcel->getActiveSheet());