Skip to content Skip to sidebar Skip to footer

Javascript - Export Image Into Excel

Using this function, I can export image into excel(XLS). This code require physical location of the image. But instead of physical path, I want to pass dataUri of the image. e.g. d

Solution 1:

I ran into the same problem, after several hours researching I solved it this way.

// Code HTML
 <div id="chart-container" style="background: #fff;">
                            <canvasclass="mt-5 w-100"id="doughnutChart"></canvas>
 </div>
// Code javascript// Convert the div to image (canvas)html2canvas(document.getElementById("chart-container"), {
    onrendered: function (canvas) {
        var img = canvas.toDataURL("image/jpeg", 0.9); //image data of canvasvar a = document.createElement("a"); //Create <a>//a.href = "data:image/png;base64," + ImageBase64; 
        a.href = img;
        a.download = fileName + ".jpeg"; //File name Herevar uri = 'data:application/vnd.ms-excel;base64,'
            , template = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40"><head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head><body><img src="{table}" alt="grafica" /></body></html>'
            , base64 = function (s) { returnwindow.btoa(unescape(encodeURIComponent(s))) }
            , format = function (s, c) { return s.replace(/{(\w+)}/g, function (m, p) { return c[p]; }) }

        var ctx = { worksheet: name || 'Hoja1', table: fileName + ".jpeg" }
        //window.location.href = uri + base64(format(template, ctx))var link = document.createElement("a");
        link.download = fileName+".xls";
        link.href = uri + base64(format(template, ctx));
        link.click();
    }
});

Result: enter image description here

Solution 2:

Try replacing the file url with the data url....

var ctx = { worksheet: 'Worksheet', table: '<img src="data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAA..." alt="" />' };

Solution 3:

You can not export the image in base64 format to excel. Visit this page : https://www.infragistics.com/community/forums/f/ignite-ui-for-javascript/109480/adding-image-in-excel

Post a Comment for "Javascript - Export Image Into Excel"