php phpexcel 内容靠最右边_PHPExcel右对齐图像

使用PHPExcel在Excel工作表中插入图片时,发现图片始终靠左对齐。通过设置单元格样式无法改变图片的位置。解决办法是确定图片的最大宽度和高度,然后计算偏移量,设置`setOffsetX`来使图片右对齐。
摘要由CSDN通过智能技术生成

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值