微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

PHPExcel右对齐图像

我正在尝试使用PHPExcel对齐图像,但我不能,因为图像覆盖在工作表上方.

// 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的文本对齐方式更改为右对齐,但图像仍然在左侧对齐.

解决方法:

有同样的挑战:如何正确地将图片与单元格对齐.我正在使用PHP Spreadsheet,它继承了PHP Excel.这有效:

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

它应该与PHPExcel以相同的方式工作.诀窍是将列宽转换为Excel像素,并使用offsetX从左水平列边框获得正确的偏移量.如果图片比列宽,则偏移量将为负.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐