PHPExcel 右对齐图像

标签 php image alignment 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 Excel 之后的 PHP Spreadsheet。这有效:

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 从左水平列边框获得正确的偏移量。如果图片比列宽,偏移量将为负数。

关于PHPExcel 右对齐图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25659925/

相关文章:

html - 将鼠标悬停在 img 上时,表格必须可见

alignment - Ada 中的 Alignment 子句有什么作用?

ios - Monotouch 的照片/图像查看器

css - Div 在父 block 中对齐

alignment - 行高和对齐的问题

javascript - WordPress:如何通过附件 ID 排除缩略图

php - zf2 没有使用 PHP 5 "DateTime"类

php - 无法在Google数据存储区中使用一种获取所有实体(使用PHP)

php - OOP 新手,如何将类公共(public)变量设置为全局变量以用于静态方法?

WPF:仅在非透明部分检测图像单击