PHPExcel右对齐图像

时间:2014-09-04 07:46:18

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

4 个答案:

答案 0 :(得分:3)

遇到同样的挑战:如何将图片右对齐到单元格。我正在使用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从左水平列边框获得正确的偏移量。如果图片比列宽,则偏移量将为负。

答案 1 :(得分:2)

这是一个想法:

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

// Logo
$maxWidth = 700;
$maxHeight = 40;

以下是代码的其余部分:

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

希望这有帮助。

答案 2 :(得分:0)

手动设置行的高度

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

$index是行号。

答案 3 :(得分:0)

您可以按照以下代码使用相对于角度点的(X,Y)的两行特殊代码将图像移向中心

更改X的位置 $objDrawing->setOffsetX(100);

更改Y的位置 $objDrawing->setOffsetY(20);

$gdImage = imagecreatefromjpeg('image/issam.jpeg');      // path for the picture 
$objDrawing = new PHPExcel_Worksheet_MemoryDrawing();
$objDrawing->setName('Logo Absiskey');                   // the name
$objDrawing->setDescription('Logo Absiskey');            // decription 
$objDrawing->setImageResource($gdImage);
$objDrawing->setRenderingFunction(PHPExcel_Worksheet_MemoryDrawing::RENDERING_JPEG);
$objDrawing->setMimeType(PHPExcel_Worksheet_MemoryDrawing::MIMETYPE_DEFAULT);
$objDrawing->setHeight(60);
$objDrawing->setWorksheet($objPHPExcel->getActiveSheet());
$objDrawing->setCoordinates('B10');
$objDrawing->setOffsetX(100);                            // set x position 
$objDrawing->setOffsetY(20);                             // set Y position