How to Insert Pictures in Excel using Node.js

How to Insert Pictures in Excel using Node.js

There are common daily tasks such as uploading images, updating pictures, and data entry. You can automate these tasks using a library. In this post we use the JS Excel library to show how to insert pictures in Excel using Node.js. After reading, you will be able to write code that inserts images into a worksheet programmatically.

We will be covering the following sections in this guide:

JS Excel library installation

The installation process of this library is developer‑friendly. Set it up by downloading the API package or running the following commands in the terminal.

npm install java
npm install aspose.cells

How to insert pictures in Excel using Node.js

Now we can write the source code and invoke methods to insert images in Excel files programmatically.

Follow these steps:

  1. Create an instance of the Workbook class.
  2. Call the getWorksheets method to obtain the WorksheetCollection.
  3. Retrieve the first worksheet with the get method.
  4. Use the add method to place a picture at the cell located at row 5, column 5.
  5. Access the picture element with the get method.
  6. Save the Excel file using the save method.

Copy & paste the following code into your main file:

The result is shown in the image below:

Insert Pictures in Excel

Picture positioning

You can further configure the image placement with advanced methods.

Steps to position the picture programmatically:

  1. Instantiate a Workbook object.
  2. Add a new sheet using the add method.
  3. Access the worksheet with the get method.
  4. Add the picture to the collection via the add method.
  5. Retrieve the picture element with the get method.
  6. Set the horizontal offset using setUpperDeltaX (0‑1024).
  7. Set the vertical offset using setUpperDeltaY.
  8. Save the workbook with the save method.

Copy & paste the following code into your main file:

The output appears in the image below:

Picture positioning

Get a Free License

You can obtain a free temporary license to try the API without evaluation limitations.

Summing up

You have learned how to insert pictures in Excel using Node.js and how to manipulate picture positioning with the JS Excel library. For more features, visit the documentation.

We also recommend following our Getting Started guide.

Stay tuned to conholdate.com for new blog posts and updates.

Ask a question

Post your questions or queries on our forum.

FAQs

How do I insert an image into an XLSX file?

Please follow this link to see the code snippet that inserts pictures in an Excel file programmatically.

See Also