Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to insert a picture into a cell? #1860

Closed
MuGongziya opened this issue Feb 18, 2021 · 10 comments
Closed

How to insert a picture into a cell? #1860

MuGongziya opened this issue Feb 18, 2021 · 10 comments

Comments

@MuGongziya
Copy link

When I insert the picture, it's not embedded in the cell.

QQ截图20210218162728

@MarkBaker
Copy link
Member

No, because Excel doesn't embed pictures in cells; they're "overlaid" above the cells, but linked to the cells.

When you add an image, you link it to a cell address:

// Add a drawing to the worksheet
$drawing = new Drawing();
$drawing->setName('PhpSpreadsheet logo');
$drawing->setDescription('PhpSpreadsheet logo');
$drawing->setPath(__DIR__ . '/../images/PhpSpreadsheet_logo.png');
$drawing->setHeight(36);
$drawing->setCoordinates('D24');
$drawing->setOffsetX(10);
$drawing->setWorksheet($spreadsheet->getActiveSheet());

Links the added image to cell address D24

@MuGongziya
Copy link
Author

不可以,因为Excel不会将图片嵌入单元格中。它们“重叠”在单元格上方,但链接到单元格。

添加图像时,将其链接到单元地址:

// Add a drawing to the worksheet
$drawing = new Drawing();
$drawing->setName('PhpSpreadsheet logo');
$drawing->setDescription('PhpSpreadsheet logo');
$drawing->setPath(__DIR__ . '/../images/PhpSpreadsheet_logo.png');
$drawing->setHeight(36);
$drawing->setCoordinates('D24');
$drawing->setOffsetX(10);
$drawing->setWorksheet($spreadsheet->getActiveSheet());

将添加的图像链接到单元格地址 D24

thanks

@MuGongziya
Copy link
Author

No, because Excel doesn't embed pictures in cells; they're "overlaid" above the cells, but linked to the cells.

When you add an image, you link it to a cell address:

// Add a drawing to the worksheet
$drawing = new Drawing();
$drawing->setName('PhpSpreadsheet logo');
$drawing->setDescription('PhpSpreadsheet logo');
$drawing->setPath(__DIR__ . '/../images/PhpSpreadsheet_logo.png');
$drawing->setHeight(36);
$drawing->setCoordinates('D24');
$drawing->setOffsetX(10);
$drawing->setWorksheet($spreadsheet->getActiveSheet());

Links the added image to cell address D24

Hello, I still have this situation after I use your code.
This is my code. Please help me to have a look.
I don't know what went wrong.
I tried wps2019 and wps2016, and the effect was the same.
QQ截图20210219080701
QQ截图20210219081128

@MAKS-dev
Copy link

Please explain what you want to do and what you think is wrong.
It seems correct, the result is what your code should do.
Please note that this library still has a (very old) bug when using the same image multiple times, see #1153

@MuGongziya
Copy link
Author

请说明您想做什么和您认为错了。
似乎正确,结果就是您的代码应该执行的操作。
请注意,当多次使用同一张图片时,该库仍存在一个(很旧的)错误,请参阅#1153

I mean, after using the code you provided. This is still the case: the picture overlaps above the cell. I loop the contents of the database into the cells. The image is stored in the server.

@MarkBaker
Copy link
Member

That's the point; Excel does not store its images in the cells, but in a layer on top of the cell; so yes, images can overlap unless you size them correctly so that they "fit" the cell dimensions

@MuGongziya
Copy link
Author

这才是重点; Excel不会将其图像存储在单元格中,而是存储在单元格顶部的一层中。所以是的,除非您正确调整尺寸,以使它们“适合”像元尺寸,否则图像可能会重叠

Okay, I already know what to do. I will set the size of the cell to resize the inserted picture. But when I insert a picture, office retains the aspect ratio of the picture. Can PhpSpreadsheet resize the picture to insert it into the cell at will?
Thank you very much for your help. Thank you.
In fact, I am Chinese. I chat with you through translation software.

@Mymko
Copy link

Mymko commented Mar 9, 2021

The Drawing class has height and width attributes.

$drawing->setHeight($value);
$drawing->setWidth($value);

Or you need to increase the column width or row height.

You might be helped by displaying a picture in a comment (when hovering over a cell), but this functionality is not implemented.

@stale
Copy link

stale bot commented Jun 26, 2021

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.
If this is still an issue for you, please try to help by debugging it further and sharing your results.
Thank you for your contributions.

@stale stale bot added the stale label Jun 26, 2021
@oleibman
Copy link
Collaborator

Closing. No update since March, and the instructions already provided seem adequate. Also, PR #2422 now allows use of image with comments.

@oleibman oleibman removed the stale label Jul 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

5 participants