Amit P.
Amit P.

Reputation: 23

How can I export image in Excel using Javascript?

One of my requirement is I want to export image and store inside excel using javascript. I am using export excel using saveAs(blob, filename) and I am able to export javascript data into the excel but I could not able to export image? There is no any server side. Any body has any idea over this

Upvotes: 0

Views: 2015

Answers (2)

Hien Nguyen
Hien Nguyen

Reputation: 18973

You can use xlsx library https://www.npmjs.com/package/xlsx

$("[id$=mybutton]").click(function(e) {
   window.open('data:application/vnd.ms-excel,' + encodeURIComponent( $('div[id$=image]').html()));
    e.preventDefault();
});

Demo https://jsfiddle.net/viethien/dfb3n2x1/11

Upvotes: 0

sidverma
sidverma

Reputation: 1185

There is a JS library to create excel. I haven't tried it though. https://github.com/stephenliberty/excel-builder.js However, it should be fairly trivial to loop through your JSON and create a CSV file, which will ultimately open in excel. That might work better depending on your needs.

But to be clear, when you say store an image, do you mean a link to an image, or a base64 text of an image? The latter would be huge and probably break excel in some way.

Other Resources: https://www.grapecity.com/blogs/how-to-importexport-excel-files-using-javascript-and-spread-sheets

Upvotes: 0

Related Questions