7

开发将使用 PHP Excel 创建 Excel 文件的 PHP 代码。

我需要将图像放入 XLS 文件中。我遇到的问题是图像的尺寸不是我在代码中定义的尺寸:

$objDrawing = new PHPExcel_Worksheet_Drawing();
$objDrawing->setName('Water_Level');
$objDrawing->setDescription('Water_Level');
$objDrawing->setPath('img/logo-rab.jpg');
$objDrawing->setHeight(74);
$objDrawing->setCoordinates('A1');
$objDrawing->setWorksheet($objPHPExcel->getActiveSheet());

此图像远高于 74 像素。也尝试添加高度和宽度,但始终相同。

你能帮我如何在我的 XLS 中添加图像,尺寸是我在 PHP 代码中定义的吗?

先感谢您!

4

5 回答 5

6

Have you tried using setWidthAndHeight and set ResizeProportional true?

$objDrawing->setWidthAndHeight(148,74);
$objDrawing->setResizeProportional(true);
于 2014-10-01T08:34:29.637 回答
5

我以不同的方式尝试过,但 excel 会自动强制图像缩放。无论如何,我通过首先将“调整大小比例”设置为错误然后为图像设置自定义宽度来解决它

/* ADD LOGO */
$objDrawing = new PHPExcel_Worksheet_Drawing();
$objDrawing->setName('Logo');
$objDrawing->setDescription('Logo');
$objDrawing->setPath('../images/logoexcel.png');
$objDrawing->setCoordinates('A1');
// set resize to false first
$objDrawing->setResizeProportional(false);
// set width later
$objDrawing->setWidth(45);
$objDrawing->setWorksheet($objPHPExcel->getActiveSheet());
$objPHPExcel->getActiveSheet()->getRowDimension(1)->setRowHeight(35);
/* END LOGO */
于 2015-11-05T10:41:55.257 回答
1

同样的问题,对我来说,我必须像这样设置 setImageResource 后设置 var

        ... 
        $this->objPHPExcel->getActiveSheet()->SetCellValue($index, $entry);
        $this->gdImage = imagecreatefromjpeg('/path/img.jpg');
        $this->objDrawing->setCoordinates('C1');
        ...
        }


        function save_excel(){
        $this->objDrawing->setRenderingFunction(PHPExcel_Worksheet_MemoryDrawing::RENDERING_JPEG);
        $this->objDrawing->setMimeType(PHPExcel_Worksheet_MemoryDrawing::MIMETYPE_DEFAULT);    
        $this->objDrawing->setImageResource($this->gdImage);
        $this->objDrawing->setHeight(100);  
于 2015-10-23T16:14:59.063 回答
0

在 PhpSpreadSheet 中类似于 PHPExcel:

$drawing = new Drawing();

$drawing->setName( $name );
$drawing->setDescription( $name );
$drawing->setPath( $path );
$drawing->setWorksheet( $sheet );
$drawing->setCoordinates( $cell );

$drawing->setHeight(100);
$drawing->setResizeProportional(true);
于 2019-02-05T11:13:44.960 回答
-2

添加这个,可能这会工作

$objDrawing->setOffsetX(110);

于 2013-08-15T12:08:12.933 回答