English 中文(简体)
Import excel files with image in php/mysql
原标题:

I want to make an import script which allows users to upload their excel file (extension not important) to my php application.

The application should reconize a list of items (so far so good).

The difficulty in this case is that the excel files contain images...

I ve read information about phpexcel library but it does not say anything about images.

Anybody ideas?

问题回答

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.

PHPExcel does support images, although it doesn t yet support charts

EDIT

Chart reader support added for Excel 2007+ .xlsx files in version 1.7.7 of PHPExcel

To import data from Excel (or any other program that can produce a text file) is very simple using the LOAD DATA command from the MySQL Command prompt.

1) Save your Excel data as a csv file (In Excel 2007 using Save As) Check the saved file using a text editor such as Notepad to see what it actually looks like, i.e. what delimiter was used etc.

2) LOAD DATA LOCAL INFILE ‘C: empyourfile.csv’ INTO TABLE database.table FIELDS TERMINATED BY ‘;’ ENCLOSED BY ‘”‘ LINES TERMINATED BY ‘ ’ (field1, field2);

3) To store image to have to use TO_BASE64

Done!

Reference





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签