Javascript - 将图像导出为ex​​cel

时间:2017-05-11 21:35:52

标签: javascript excel nvd3.js

使用此功能,我可以将图像导出到Excel(XLS)。此代码需要图像的物理位置。但是,我想要传递图像的dataUri,而不是物理路径。例如数据:图像/ JPEG; BASE64,/ 9J / 4AAQSkZJRgABAQAAAQABAA ...

有可能吗?

function exportExcel() {

    var template = { excel: '<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><meta name=ProgId content=Excel.Sheet> <meta name=Generator content="Microsoft Excel 11"><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><!--[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><table>{table}</table></body></html>' };

    var format = function (s, c) {
        return s.replace(new RegExp("{(\\w+)}", "g"), function (m, p) {
            return c[p];
        });
    };

    var base64 = function (s) {
        return window.btoa(window.unescape(encodeURIComponent(s)));
    };

    var ctx = { worksheet: 'Worksheet', table: '<img src="file://C:/Temp/chart.png" alt="" />' };

    var b64 = base64(format(template.excel, ctx));

    blob = b64toBlob(b64, 'application/vnd.ms-excel');
    var blobUrl = window.URL.createObjectURL(blob);

    var saveLink = document.createElement('a');

    saveLink.download = 'export.xls';
    saveLink.href = blobUrl;
    saveLink.click();
}


function b64toBlob(b64Data, contentType, sliceSize) {
    contentType = contentType || '';
    sliceSize = sliceSize || 512;

    var byteCharacters = window.atob(b64Data);
    var byteArrays = [];

    var offset;
    for (offset = 0; offset < byteCharacters.length; offset += sliceSize) {
        var slice = byteCharacters.slice(offset, offset + sliceSize);

        var byteNumbers = new Array(slice.length);
        var i;
        for (i = 0; i < slice.length; i = i + 1) {
            byteNumbers[i] = slice.charCodeAt(i);
        }

        var byteArray = new window.Uint8Array(byteNumbers);

        byteArrays.push(byteArray);
    }

    var blob = new window.Blob(byteArrays, {
        type: contentType
    });
    return blob;
}

附上要求的截图。 enter image description here 谢谢!

3 个答案:

答案 0 :(得分:1)

经过几个小时的研究,我遇到了同样的问题。

// Code HTML
 <div id="chart-container" style="background: #fff;">
                            <canvas class="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 canvas

        var a = document.createElement("a"); //Create <a>
        //a.href = "data:image/png;base64," + ImageBase64; 
        a.href = img;
        a.download = fileName + ".jpeg"; //File name Here

        var 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) { return window.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();
    }
});

结果: enter image description here

答案 1 :(得分:0)

尝试用数据网址替换文件网址....

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

答案 2 :(得分:0)

您无法将base64格式的图像导出为Excel。 访问此页面:https://www.infragistics.com/community/forums/f/ignite-ui-for-javascript/109480/adding-image-in-excel