phpexcel在合并的单元格中插入多个图像


phpexcel insert multiple images in merged cells

原谅我英语不好,我是一名来自亚洲的程序员。我想在合并的单元格中插入多个图像,但所有图像都是重叠的。所以我写这样的代码:

//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;
   }
}

我希望使用"offsetY"在垂直方向上分隔每个图像,但所有图像都挤在一起。我认为原因是我使用了"$objDrawing->setCoordinates($cell);",所有图像都只在$cell位置。我想设置所有的图像按照顺序和间隔排列。有人能帮我吗?

原因是(根据此)OffsetY特定于单元格。

不幸的是,我想不出解决办法。也许问问他们。

此函数适用于我。它从所需的位置X和行号返回一个带有坐标和偏移X的数组。

也许有人帮忙。

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];