8

我想制作一个导入脚本,允许用户将他们的 excel 文件(扩展名不重要)上传到我的 php 应用程序。

应用程序应该重新整理项目列表(到目前为止一切都很好)。

在这种情况下,困难在于 excel 文件包含图像......

我已经阅读了有关 phpexcel 库的信息,但它没有说明任何有关图像的信息。

任何人的想法?

4

3 回答 3

14

You can access images by PHPExcel library.

For importing images:

$objPHPExcel = PHPExcel_IOFactory::load("MyExcelFile.xls");

foreach ($objPHPExcel->getSheetByName("My Sheet")->getDrawingCollection() as $drawing) {
    if ($drawing instanceof PHPExcel_Worksheet_MemoryDrawing) {
        ob_start();
        call_user_func(
            $drawing->getRenderingFunction(),
            $drawing->getImageResource()
        );
        $imageContents = ob_get_contents();
        ob_end_clean();
    }
}

Then $imageContents will contain the image data that you can output or save as a file etc. You can see this question: PHPExcel: How to insert an image in the first page header and enlarge it to fit it's content?

For accessing images:

ArrayObject of all the image objects in the active worksheet will return by:

$objPHPExcel->getActiveSheet()->getDrawingCollection() ;

These objects will be either PHPExcel_Worksheet_Drawing or PHPExcel_Worksheet_MemoryDrawing objects: you can identify which using is_a(). You can then use the methods appropriate to that class (as described in the API) either to read the image data from file (for PHPExcel_Worksheet_Drawing objects) or directly from the PHPExcel_Worksheet_MemoryDrawing object itself. The getName() and getDescription() methods can be used to retrieve the relevant values fro the image object.

Now it's also possible to have image objects associated with print headers:

$objPHPExcel->getActiveSheet()->getHeaderFooter()->getImages() 

can be used to retrieve images from the header/footer. This is an array of PHPExcel_Worksheet_HeaderFooterDrawing objects. All the PHPExcel_Worksheet_Drawing methods can be used to extract the image file from these objects.

于 2012-03-21T06:49:29.720 回答
1

PHPExcel 确实支持图像,虽然它还不支持图表

编辑

在 PHPExcel 1.7.7 版中添加了对 Excel 2007+ .xlsx 文件的图表阅读器支持

于 2010-06-15T14:57:51.147 回答
1

使用 MySQL 命令提示符下的 LOAD DATA 命令从 Excel(或任何其他可以生成文本文件的程序)导入数据非常简单。

1) 将您的 Excel 数据另存为 csv 文件(在 Excel 2007 中使用另存为)使用文本编辑器(例如记事本)检查保存的文件以查看其实际外观,即使用的分隔符等。

2) LOAD DATA LOCAL INFILE 'C:\temp\yourfile.csv' INTO TABLE database.table FIELDS TERMINATED BY ';' 由 '"' 包围的行由 '\r\n' (field1, field2) 终止;

3) 存储图像必须使用TO_BASE64

完毕!

参考

于 2012-03-28T05:43:33.463 回答