Correct image alignment PHPExcel

I am trying to align an image using PHPExcel, but I cannot, because the image is overlaid on a sheet.

// Create new picture object
  $objDrawing = new PHPExcel_Worksheet_Drawing();
  $objDrawing->setPath('my_img.jpg');

// Insert picture
  $objDrawing->setCoordinates('A1');
  $objDrawing->setWorksheet($objPHPExcel->getActiveSheet());

// Style cell
  $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);

The alignment of the text in A1 changes to the right, but the image is still aligned to the left.

+2
source share
2 answers

Here is an idea:

You need to determine the maximum width / height (by experimenting). Save the values.

// Logo
$maxWidth = 700;
$maxHeight = 40;

Here is the rest of the code:

$objDrawing = new PHPExcel_Worksheet_Drawing();
$objDrawing->setWorksheet($objPHPExcel->getActiveSheet());
$objDrawing->setName("Logo");
$objDrawing->setDescription("Company Logo");
$objDrawing->setPath('logo.png');
$objDrawing->setCoordinates('A1');
$objDrawing->setHeight($maxHeight);
// This is the "magic" formula
$offsetX =$maxWidth - $objDrawing->getWidth();
$objDrawing->setOffsetX($offsetX);

Hope this helps.

+2
source

Set the line height manually

$objPHPExcel->getActiveSheet()->getRowDimension($index)->setRowHeight(100);

$index - line number.

0
source

Source: https://habr.com/ru/post/1671583/


All Articles