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

时间:2023-01-20 09:48:29

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作为值。

3 个解决方案

#1


9  

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');

#2


8  

"Trigun", your suggestion really helped. I was able to download the latest PHPExcel Classes from http://phpexcel.codeplex.com/releases and 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.

"三枪",你的建议真的很有帮助。我可以从http://phpexcel.codeplex.com/release中下载最新的PHPExcel类,很快就可以运行了。然而,花了一些额外的时间来弄清楚如何向excel文件添加图像。你的解释没有多大帮助。

Here is a complete description on how to do it:

以下是如何做到这一点的完整描述:

First, download the library and place it in a logical place in 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类文件:

/** 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)创建一个“绘图”对象,我们可以将图像加载到其中。请记住在web服务器中使用有效的图像URL替换图像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!

这就是全部!

#3


0  

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

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

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

#1


9  

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');

#2


8  

"Trigun", your suggestion really helped. I was able to download the latest PHPExcel Classes from http://phpexcel.codeplex.com/releases and 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.

"三枪",你的建议真的很有帮助。我可以从http://phpexcel.codeplex.com/release中下载最新的PHPExcel类,很快就可以运行了。然而,花了一些额外的时间来弄清楚如何向excel文件添加图像。你的解释没有多大帮助。

Here is a complete description on how to do it:

以下是如何做到这一点的完整描述:

First, download the library and place it in a logical place in 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类文件:

/** 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)创建一个“绘图”对象,我们可以将图像加载到其中。请记住在web服务器中使用有效的图像URL替换图像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!

这就是全部!

#3


0  

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

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

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