PHPExcel右对齐图像

I'm trying to align an image using PHPExcel but i can't because the image is overlaid above the worksheet.

// 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 text align of A1 change to right align but the image it still aligned on the left.

Here is an idea:

You need to determine the maximum width / height (by experimentation). 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.

Set the height of the row manually

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

$index is row number.

Had the same challenge: How to right align a picture to a cell. I am using PHP Spreadsheet which succeeds PHP Excel. This works:

use PhpOffice\PhpSpreadsheet\Worksheet;
use PhpOffice\PhpSpreadsheet\Worksheet\Drawing;

$ws = $spreadsheet->getActiveSheet(); 
$logoPath = __DIR__ . '/reporting/templates/SmallLogo.jpg';
$drawing = new Drawing();
$drawing->setPath($logoPath);
$drawing->setCoordinates('K2');
$drawing->setHeight(45); //pixels
$colWidth = $ws->getColumnDimension('K')->getWidth();
if ($colWidth == -1) { //not defined which means we have the standard width
    $colWidthPixels = 64; //pixels, this is the standard width of an Excel cell in pixels = 9.140625 char units outer size
} else {                  //innner width is 8.43 char units
    $colWidthPixels = $colWidth * 7.0017094; //colwidht in Char Units * Pixels per CharUnit
}
$offsetX = $colWidthPixels - $drawing->getWidth(); //pixels
$drawing->setOffsetX($offsetX); //pixels
$drawing->setWorksheet($ws);

It should work the same way with PHPExcel. The trick is to convert the column width into Excel pixels and use offsetX to get the right offset from the left horizontal column border. The offset will be negative if the picture is wider than the column.