PHPExcel右对齐图像 [英] PHPExcel right align image

查看:633
本文介绍了PHPExcel右对齐图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用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);

A1的文本对齐方式更改为右对齐,但图像仍然在左侧对齐。

The text align of A1 change to right align but the image it still aligned on the left.

推荐答案

这是一个想法:

你需要确定最大宽度/身高(通过实验)。保存值。

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

希望这会有所帮助。

这篇关于PHPExcel右对齐图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆