Patrick62
Patrick62

Reputation: 71

How to skip several lines of an Excel file in php?

I have an Excel file (xlsx) with several pictures

And I have this php script that allows me to extract the photos contained in it, insert them in a folder and rename them according to the name of the cells in front of each photo. The script works but I would like to start reading the lines from line 5. I tried with $i <= 3 to skip the first 4 lines but this causes a shift in the picture names. How can I solve my problem?

<?php

require_once 'PHPExcel/Classes/PHPExcel/IOFactory.php';
$path = 'C:/wamp64/www/Extract_pictures_Excel/imagetest.xlsx';
$objPHPExcel = PHPExcel_IOFactory::load($path);

$i = 0;

foreach ($objPHPExcel->getActiveSheet()->getDrawingCollection() as $drawing ) {
    $i++;
  

    if ($drawing instanceof PHPExcel_Worksheet_MemoryDrawing) {
        ob_start();
        call_user_func(
            $drawing->getRenderingFunction(),
            $drawing->getImageResource()
        );
        $imageContents = ob_get_contents();
        ob_end_clean();
        switch ($drawing->getMimeType()) {
            case PHPExcel_Worksheet_MemoryDrawing::MIMETYPE_PNG :
                    $extension = 'png'; break;
            case PHPExcel_Worksheet_MemoryDrawing::MIMETYPE_GIF:
                    $extension = 'gif'; break;
            case PHPExcel_Worksheet_MemoryDrawing::MIMETYPE_JPEG :
                    $extension = 'jpg'; break;   
     }
    } 

    else {
        $zipReader = fopen($drawing->getPath(),'r');
        $imageContents = '';
        while (!feof($zipReader)) {
            $imageContents .= fread($zipReader,1024);
        }
        fclose($zipReader);
        $extension = $drawing->getExtension();
        $chemin = 'C:/wamp64/www/Extract_pictures_Excel/images/';
     
    }

    $sheet = $objPHPExcel->getActiveSheet();
    foreach ($sheet->getDrawingCollection() as $drawing) {
    $row = (int)substr($drawing->getCoordinates(), 1);

  // retrieve the image data anyway you like

    $stylecode = $sheet->getCell('B'.$row)->getValue();
    $colorcode = $sheet->getCell('C'.$row)->getValue();
    $finalname = $stylecode.'_'.$colorcode;
    $myFileName = $chemin.$finalname.'.'.$extension;
    file_put_contents($myFileName,$imageContents);
 
  }
}
 ?>

Upvotes: 0

Views: 478

Answers (1)

Δ O
Δ O

Reputation: 3710

I think getDrawingCollection() gives you a list of all the drawings on the active sheet, but this list is not directly related to cell coordinates. So skipping some items of this list is not the same as skipping rows.

Not sure how this goes with the old PHPExcel libs, but with current PhpOffice\PhpSpreadsheet, a Drawing or BaseDrawing objects should have their $coordinates property.

Example:

$sheet = $objPHPExcel->getActiveSheet();
foreach ($sheet->getDrawingCollection() as $drawing) {
  $row = (int)substr($drawing->getCoordinates(), 1);

  // retrieve the image data anyway you like

  $stylecode = $sheet->getCell('B'.$row)->getValue();
  $colorcode = $sheet->getCell('C'.$row)->getValue();
  $finalname = $stylecode.'_'.$colorcode;

  ...
}

You should parse the $drawing->coordinates in order to retrieve values of it's neighbouring cells.

Upvotes: 1

Related Questions