How to Insert Pictures in Excel using Node.js

How to Insert Pictures in Excel using Node.js

There are some common tasks that you do on daily bases such as uploading images, updating the existing pictures, data entry, and many more. You can automate these tasks programmatically using any available library. In this blog post, we will go with this JS Excel library to learn how to insert pictures in Excel using Node.js. After going through this article, you will be able to write a code snippet 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 quite developer-friendly. You can easily set up it 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 the steps and the code snippet mentioned below:

  1. Create an instance of the Workbook class.
  2. Invoke the getWorksheets method that gets the WorksheetCollection collection in the spreadsheet.
  3. Obtain the reference of the first worksheet by calling the get method.
  4. Call the add method to add a picture at the location of a cell whose row and column indices are 5 in the worksheet.
  5. get the picture element at the specified index.
  6. Saving the Excel file by calling the save method.

Copy & paste the following code into your main file:

You may see the output in the image below:

Insert Pictures in Excel

Picture positioning

In addition, we may configure our code snippet further by employing advanced methods.

You can follow the steps to position the image programmatically:

  1. Instantiating a Workbook object.
  2. Adding a new sheet by calling the add method.
  3. Call the get method to access the Worksheet element at the specified index.
  4. Invoke the add method to add the picture to the collection.
  5. Access the picture element at the specified index by calling the get method.
  6. Call the setUpperDeltaX method to set the shape’s horizontal offset from its upper left corner column. The range of value is 0 to 1024.
  7. Set the shape’s vertical offset from its upper left corner row by calling the setUpperDeltaY method.
  8. Invoke the save method to save the Excel file.

Copy & paste the following code into your main file:

The output can be seen in the image below:

Picture positioning

Get a Free License

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

Summing up

This is the end of this tutorial blog post. You have gone through how to insert pictures in Excel using Node.js. Further, you also have gone through that how to manipulate pictures using this JS Excel library. In addition, you may visit the documentation to get awareness about the other features.

Moreover, we suggest you follow our Getting Started guide.

Finally, conholdate.com is writing new blog posts. Therefore, please stay in touch for regular updates.

Ask a question

You can let us know about your questions or queries on our forum.

FAQs

How do I insert an image into an XLSX file?

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

See Also