Closed Sk-0515 closed 1 month ago
I'm offline for the next week so I can't show an example but the basic approach would be to set the cell height and width in pixels, then scale the images to the cell height and half width and insert the first image with zero offset and the second image with the half width offset. The methods you should use/look at are:
https://docs.rs/rust_xlsxwriter/latest/rust_xlsxwriter/struct.Image.html#method.set_scale_to_size
@jmcnamara
Follow your steps, but the second image will overwrite the previous one!
Could you add the code instead of an image of the code and could you add the sample image.
Also, could you try with the keep_aspect_ratio
parameter in Image::set_scale_to_size()
set to true
.
Actually, the issue is probably that the crate stores the images by (row, column)
tuple and therefore only one image per cell.
The only way around this would be to place the second image in the cell to the left or right and use the x_offset
to position it in the target cell.
let mut workbook = Workbook::new();
let worksheet = workbook.add_worksheet();
// set the cell height and width in pixels
worksheet.set_default_row_height_pixels(30);
worksheet.set_column_width_pixels(1, 100)?;
worksheet.set_row_height_pixels(1,100)?;
worksheet.write(0, 0, "Age")?;
worksheet.write(0, 1, "Name")?;
worksheet.write(1, 0, 18)?;
let mut img = Image::new(Path::new("static/avatar.png"))?;
// scale the images to the cell height and half width
img = img.set_scale_to_size(50, 100, false);
// insert the first image with zero offset and the second image with the half width offset
worksheet.insert_image_with_offset(1, 1, &img, 0, 0)?;
worksheet.insert_image_with_offset(1, 0, &img, 100, 0)?;
workbook.save("static/image.xlsx")?;
If it is placed in the left cell, which is at the boundary point, what should I do with other images? Placing it in the right cell is not possible because x_offset cannot receive i32
Change your approach and use merging cells to solve this problem. Thank you for your help @jmcnamara
Question
How to Insert Multiple Images into a Cell?