简体   繁体   中英

phpexcel insert multiple images in merged cells

Forgive my poor English,I'm a programmer from Asia. I want to insert multiple images in merged cells,but all the images are overlapping.So I write code like this:

//merge cells
$column = 0;
$cell = $position[$column].$row;
$merge_str = $position[$column] . $row . ":" . $position[$column] . $last_row;
$objExcel->getActiveSheet()->mergeCells($merge_str);
$cell_value = '';
$objExcel->setExcelFontFormat($cell, $cell_value, $font_size, false, 'left', 'center');

$offSetY = 10;

//loop $export_data_item['images_path'] ,$image_nums is the mount of images
for($i=0;$i<$image_nums;$i++){
   if(file_exists($export_data_item['images_path'][$i])){
        $objDrawing = new PHPExcel_Worksheet_Drawing();
        $objDrawing->setPath($export_data_item['images_path'][$i]);

        $objDrawing->setOffsetX(10);

        $objDrawing->setOffsetY($offSetY);
        $objDrawing->setRotation(15);
        $objDrawing->setHeight($export_data_item['images'][$i]['height']);
        $objDrawing->setWidth($export_data_item['images'][$i]['width']);

        $objDrawing->setCoordinates($cell);
        $objDrawing->setWorksheet($objExcel->getActiveSheet());

        $offSetY = $export_data_item['images'][$i]['height'] + $offSetY + 10;
   }
}

I hope use 'offsetY' to space every images in vertical direction,but all images squeezed together.I think the reason is that I use "$objDrawing->setCoordinates($cell);", all images only in the $cell position. I want to set all images be arranged according to the sequence and interval.Someone can help me?

Reason is (according to this ) that OffsetY is specific to the cell.

Unfortunately I can't think of a solution. Ask them maybe.

This function works for me. It returns an array with Coordinate and OffsetX from a desired Position X and row number.

Maybe someone helps.

function getCoordOffx($posX, $row) {//Parameters: (desired Position X, row number)
    global $objPHPExcel;
    $cpos=$widthpix=0;//Set to 0 Current position and width pixels
    $col=1;//First column
    $colname="A";//If posX=0 not enter in while, and assign colname=A
    while ($posX>$cpos) {
        $colname=chr(64+$col);//Convert column to Letter. chr(65)=A
        $width=$objPHPExcel->getActiveSheet()->getColumnDimension($colname)->getWidth(); //Get width of Column
        $font=$objPHPExcel->getActiveSheet()->getStyle($colname.$row)->getFont();//Get Font of column and row
        $widthpix= PHPExcel_Shared_Drawing::cellDimensionToPixels($width,$font); // convert to pixels
        $cpos+=$widthpix;//Add pixels of current column to cpos
        $col++;//Next column
    }
    $offsX=(int)($posX-($cpos-$widthpix));//Offset is Desired Position X minus start of current column
return array($colname,$offsX);//Returns Column Name Letter (A or C or E...) and OffsetX
}

$coAndOf=getCoordOffx(195,3); //Desired Position X=195, row = 3
$colL=$coAndOf[0];
$offX=$coAndOf[1];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM