导出带有标题的Excel,包括图像

Export excel with header including image

本文关键字:Excel 包括 图像 标题      更新时间:2023-09-26

我正在使用以下代码导出项目中的 Excel 文件:

var tableToExcel = (function() {
    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]--><meta http-equiv="content-type" content="text/plain; charset=UTF-8"/></head><body><table>{table}</table></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]; 
            }) 
        }
    return function(table, name) {
        if (!table.nodeType) 
            table = document.getElementById(table)
        var ctx = {
            worksheet: name || 'Worksheet', 
            table: table.innerHTML
        }
        var blob = new Blob([format(template, ctx)]);
        var blobURL = window.URL.createObjectURL(blob);
        return blobURL;
    }
})()
$("#btnExport").click(function () {
    var todaysDate = moment().format('DD-MM-YYYY');
    var blobURL = tableToExcel('account_table', 'test_table');
    $(this).attr('download', todaysDate + '.xls')
    $(this).attr('href', blobURL);
});

小提琴

现在我想在我的工作表中插入一个标题,包括这样的图片。我找不到与此相关的任何内容,这可能吗?

谢谢。

如果你想要这样的东西https://jsfiddle.net/r8bx18kx/加

 <img> <h1> etc

在顶部并给出图像URL,还可以将内联样式应用于元素以进行精确布局。