PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, October 5, 2022

[FIXED] How can I save an excel file containing images with different row height in different cells?

 October 05, 2022     excel, php, phpexcel     No comments   

Issue

I am trying out PHPExcel and based on its examples I have written this code:

<?php

require_once "QueryHandler.php";

define('EOL',(PHP_SAPI == 'cli') ? PHP_EOL : '<br />');

/** Include PHPExcel */
require_once 'PHPExcel/Classes/PHPExcel.php';


// Create new PHPExcel object
$objPHPExcel = new PHPExcel();

// Set document properties
$objPHPExcel->getProperties()->setCreator("Lajos Arpad")
                             ->setLastModifiedBy("Lajos Arpad")
                             ->setTitle("Import Test Foto")
                             ->setSubject("Import Test Foto")
                             ->setDescription("Import Test Foto")
                             ->setKeywords("office PHPExcel php")
                             ->setCategory("Test result file");


// Add some data
$objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A1', 'cod_comanda')
            ->setCellValue('B1', 'Foto 1')
            ->setCellValue('C1', 'Foto 2')
            ->setCellValue('D1', 'Foto 3')
            ->setCellValue('E1', 'Foto 4')
            ->setCellValue("F1", "Foto 5")
            ->setCellValue("G1", "Foto 6")
            ->setCellValue("H1", "Foto 7");

$objPHPExcel->getActiveSheet()->setTitle('Import Test Foto');

$toContinue = true;
$limit = 0;
$size = 100;

$pictures = QueryHandler::buildSelect("testtable", array("cod_comanda", "poze"), "Created", $limit, $size);

$index = 2;
$activeSheet = $objPHPExcel->getActiveSheet();
$columns = array("B", "C", "D", "E", "F", "G", "H");
foreach ($pictures as $picture) {
    $activeSheet->setCellValue("A".$index, $picture["cod_comanda"]);
    if (strlen($picture["poze"])) {
        $items = explode(",", $picture["poze"]);
        $itemCount = count($items);
        for ($innerIndex = 0; $innerIndex < $itemCount; $innerIndex++) {
            $activeSheet->setCellValue($columns[$innerIndex].$index, $items[$innerIndex]);
        }
    }
    $index++;
}

// Save Excel 2007 file

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save("export/Import Test Foto.xlsx");

Current output

Current output

Desired output

Desired output

It exports the kind of stuff which can be seen on the first picture. I would like to be able to export it like shown in the second picture, which involves the Foto columns being near several lines in the A column. Notice that the current code only exports textual data, while at the end I need to export the images, but I think I need to figure out how can I modify the relative height of cells compared to the first column's cells first. How can I achieve that?


Solution

If you know the resolution of your images, then you can use the methods in the PHPExcel_Shared_Drawing class to convert pixels to cell dimensions (), and vice versa



Answered By - Mark Baker
Answer Checked By - Katrina (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing