在PHPExcel中检索图像

时间:2011-10-25 09:37:33

标签: php phpexcel


我想使用PHPExcel从excel文件读取/检索图像到PHP。 此代码用于从特定单元格中检索值。

    $objPHPExcel->getActiveSheet()->getCell('B5')->getValue();

这只检索单元格值,但我无法检索图像。有没有办法做到这一点?

3 个答案:

答案 0 :(得分:8)

Googling phpexcel read image得出this page作为第二个结果。它告诉你如何做到这一点。

直接引用相关信息:

  

$objPHPExcel->getActiveSheet()->getDrawingCollection()   将返回活动工作表中所有图像对象的ArrayObject。

     

这些对象可以是PHPExcel_Worksheet_DrawingPHPExcel_Worksheet_MemoryDrawing个对象:您可以使用is_a()来识别哪些对象。然后,您可以使用适合该类的方法(如API中所述)从文件(对于PHPExcel_Worksheet_Drawing对象)或直接从PHPExcel_Worksheet_MemoryDrawing对象本身读取图像数据。 getName()getDescription()方法可用于从图像对象中检索相关值。

     

请注意,也可以将图像对象与打印标题相关联:

     

$objPHPExcel->getActiveSheet()->getHeaderFooter()->getImages()可用于从页眉/页脚中检索图像。这是一个PHPExcel_Worksheet_HeaderFooterDrawing个对象的数组。所有PHPExcel_Worksheet_Drawing方法都可用于从这些对象中提取图像文件。

答案 1 :(得分:6)

检查此示例。我发现它很有用..

$objPHPExcel = PHPExcel_IOFactory::load($_FILES['archivo']['tmp_name']);

$i = 0;
foreach ($objPHPExcel->getActiveSheet()->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();
        $extension = 'png';
    } else {
        $zipReader = fopen($drawing->getPath(),'r');
        $imageContents = '';

        while (!feof($zipReader)) {
            $imageContents .= fread($zipReader,1024);
        }
        fclose($zipReader);
        $extension = $drawing->getExtension();
    }
    $myFileName = '00_Image_'.++$i.'.'.$extension;
    file_put_contents($myFileName,$imageContents);
}

来源: http://phpexcel.codeplex.com/workitem/18189

答案 2 :(得分:0)

class ExcelImport
{
    /**
     * @var
     */
    protected $excel;
    /**
     * @var
     */
    protected $work_sheet;
    /**
     * @var array
     */
    protected $excel_data = [];
    /**
     * ExcelImport constructor.
     * @param Request $request
     * @throws \PHPExcel_Exception
     * @throws \PHPExcel_Reader_Exception
     */
    public function __construct(Request $request)
    {
        //Load file from request
        $this->excel = PHPExcel_IOFactory::load($request->file('file'));
        //Get active sheet
        $this->work_sheet = $this->excel->getActiveSheet();
    }
    /**
     * @return array
     */
    public function import()
    {
        //Iterate through drawing collection
        foreach ($this->work_sheet->getDrawingCollection() as $drawing) {
            //check if it is instance of drawing
            if ($drawing instanceof PHPExcel_Worksheet_Drawing) {
                //creating image name with extension
                $file_name = str_replace(' ', '_', $drawing->getName()).'.'.$drawing->getExtension();
                //Get image contents from path and store them in Laravel storage
                Storage::put('public/'.$file_name, file_get_contents($drawing->getPath()));
                //create images array initially
                $this->excel_data[] = [
                    'image' => $file_name
                ];
            }
        }
        //Map other data present in work sheet
        return $this->rowData();
    }
    /**
     * @return array
     */
    private function rowData()
    {
        $i = 0;
        //Iterate through row by row
        foreach ($this->work_sheet->getRowIterator(2) as $row) {
            //iterate through cell by cell of row
            foreach ($row->getCellIterator() as $cell) {
                //In case of image data that would be null continue
                //We have already populated them in array
                if(is_null($cell->getValue())){continue;}
                //Map other excel data into the array
                $this->excel_data[$i]['name'] = $cell->getValue();
            }
            $i++;
        }
        //Return final data array
        return $this->excel_data;
    }
}

来源(我的博客): https://meramustaqbil.com/2019/02/23/how-to-extract-images-from-excel-file-using-maat-websites-laravel-excel/