Monday 1 May 2017

Insert a image in a excel worksheet using xlsx - Node.js

Good day everyone,

I'm looking for anyone that can help me to solve my problem. I am trying to save an chart generated using chart.js into an excel file. Right now, I am able to save it as an png but I have no idea how to insert it in my xlsx document which is created programmatically.

This is my code that save my chart

    $('#myChart').get(0).toBlob(function(blob){
        saveAs(blob,filename);
     });

The blob variable is contains the image (I am using FileSaver for the saveAs and Canvas to Blob to get the image blob)

Here is my code to create my workbook

var wb = { SheetNames:[], Sheets:{} };
wb.SheetNames[0] = sheetName;
var worksheet = XLSX.utils.aoa_to_sheet(table);
wb.Sheets[sheetName] = worksheet;
wb.SheetNames[1] = 'ImageSheet';
wb.Sheets[wb.SheetNames[1]] = {}; <------- I had to insert an object 
                                           otherwise next line throws an exception
wb.Sheets[wb.SheetNames[1]]['!images'] =
     {
        name:'image1.jpeg',
        data: picBlob, <------------------ My blob from the code above
        opts: {base64 : true},
        position:{
            type: 'twoCellAnchor',
            attrs: {editAs:'oneCell'},
            from: { col: 2, row : 2 },
            to: { col: 6, row: 5 }
        }
    };

I took this example from the issue #509 : https://github.com/SheetJS/js-xlsx/pull/509. I tried everything and nothing seems to work

The imageSheet page is empty when I opened it up

If you need anymore information let me know!

Thank you for taking the time to read this!



via FrankClearGoals

No comments:

Post a Comment