Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Insert Image using ExcelJs into a cell

I'm trying to add an image to a specific cell, is there any way I can adjust the image size or the cell size so it can fit on the cell.?

    result.map(order => {
      const worksheet = workbook.addWorksheet(order.branch_code);

      worksheet.columns = [
        { header: 'Id', key: 'id', width: 10 },
        { header: 'Product Code', key: 'pcode', width: 20 },
        { header: 'Image', key: 'image', width: 50 },
      ];

      order.products.map(product => {
        const imageId = workbook.addImage({
          filename: `${PRODUCT_IMAGE_PATH}/${product.image}`,
          extension: 'jpeg',
        });

        worksheet.addRow({ id: product.products.id, pcode: product.products.product_code });
        worksheet.addImage(imageId, `C${position}:C${position}`);
        position++;
      });
    });

    await workbook.xlsx.writeFile('export.xlsx');

enter image description here

but it looks like this, how can i fix this.?

expected OP:

enter image description here

like image 580
Thorin Avatar asked Jul 02 '26 18:07

Thorin


1 Answers

Been asked ages ago, but for anyone stumbling on this — you can increase the height of the row like this (add right before the order.products.map(...) loop). Note that this will increase the height for all the rows.

worksheet.properties.defaultRowHeight = 20 // Increase this value to increase row height

You can also configure the image size when adding it. Just pass the ext option and give it an object with width and height properties (tl for placement, editAs for cell behavior):

worksheet.addImage(imageID, {
  tl: { col: colNum, row: rowNum },
  ext: { width: 150, height: 100 },
  editAs: 'oneCell'
});
like image 82
Ludolfyn Avatar answered Jul 05 '26 08:07

Ludolfyn



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!