jmcnamara / rust_xlsxwriter

A Rust library for creating Excel XLSX files.
https://crates.io/crates/rust_xlsxwriter
Apache License 2.0
323 stars 26 forks source link

Insert Multiple Images into a Cell #103

Closed Sk-0515 closed 1 month ago

Sk-0515 commented 1 month ago

Question

How to Insert Multiple Images into a Cell?

Snipaste_2024-08-07_21-27-11

jmcnamara commented 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/worksheet/struct.Worksheet.html#method.set_default_row_height_pixels

https://docs.rs/rust_xlsxwriter/latest/rust_xlsxwriter/worksheet/struct.Worksheet.html#method.set_column_width_pixels

https://docs.rs/rust_xlsxwriter/latest/rust_xlsxwriter/struct.Image.html#method.set_scale_to_size

https://docs.rs/rust_xlsxwriter/latest/rust_xlsxwriter/worksheet/struct.Worksheet.html#method.insert_image

https://docs.rs/rust_xlsxwriter/latest/rust_xlsxwriter/worksheet/struct.Worksheet.html#method.insert_image_with_offset

Sk-0515 commented 1 month ago

@jmcnamara

Snipaste_2024-08-08_16-11-27

Snipaste_2024-08-08_16-08-55

Follow your steps, but the second image will overwrite the previous one!

jmcnamara commented 1 month ago

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.

jmcnamara commented 1 month ago

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.

Sk-0515 commented 1 month ago
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

Sk-0515 commented 1 month ago

Change your approach and use merging cells to solve this problem. Thank you for your help @jmcnamara