如何使用PHP在动态生成的MS Excel文件中插入/嵌入图像?

时间:2021-09-07 01:22:28

I need to export a report to Excel file using PHP i.e. on the fly. I am actually getting data from MySQL and presenting it in HTML tables.

我需要使用PHP将报告导出到Excel文件,即即时。我实际上是从MySQL获取数据并将其呈现在HTML表格中。

I am able to export all this data into Excel file format, except images. I can't point the image source to an absolute URL, because this file also needs to work off-line.

我可以将所有这些数据导出为Excel文件格式,但图像除外。我无法将图像源指向绝对URL,因为此文件也需要脱机工作。

Is there any way I can embed/insert images into this Excel file without using any library i.e. only PHP code or library which doesn't require PEAR/PECL extensions. I am using LAMP stack, so COM is not an option.

有没有什么方法可以嵌入/插入图像到这个Excel文件而不使用任何库,即只需要PHP代码或不需要PEAR / PECL扩展的库。我正在使用LAMP堆栈,因此COM不是一个选项。

1 个解决方案

#1


1  

I'm not sure what your reasoning is for not using PEAR, but there is a modified version (PHP Excel Creator library) of PEAR Spreadsheet Excel Writer which does not have any dependencies on the rest of PEAR.

我不确定你不使用PEAR的原因是什么,但是PEAR Spreadsheet Excel Writer的修改版本(PHP Excel Creator库)与PEAR的其余部分没有任何依赖关系。

I know that the original PEAR version has a method Worksheet::insertBitmap() . You would need to convert the images to bitmap if they aren't already (maybe using GD in PHP)

我知道原始的PEAR版本有一个方法Worksheet :: insertBitmap()。你需要将图像转换为位图(如果它们还没有)(可能在PHP中使用GD)

If you are on Windows and have decent access to the server you could also try using COM to talk to Excel and generate the file that way.

如果您在Windows上并且可以正常访问服务器,您还可以尝试使用COM与Excel通信并以此方式生成文件。

#1


1  

I'm not sure what your reasoning is for not using PEAR, but there is a modified version (PHP Excel Creator library) of PEAR Spreadsheet Excel Writer which does not have any dependencies on the rest of PEAR.

我不确定你不使用PEAR的原因是什么,但是PEAR Spreadsheet Excel Writer的修改版本(PHP Excel Creator库)与PEAR的其余部分没有任何依赖关系。

I know that the original PEAR version has a method Worksheet::insertBitmap() . You would need to convert the images to bitmap if they aren't already (maybe using GD in PHP)

我知道原始的PEAR版本有一个方法Worksheet :: insertBitmap()。你需要将图像转换为位图(如果它们还没有)(可能在PHP中使用GD)

If you are on Windows and have decent access to the server you could also try using COM to talk to Excel and generate the file that way.

如果您在Windows上并且可以正常访问服务器,您还可以尝试使用COM与Excel通信并以此方式生成文件。