前端原生js导出excel

需求:

客户需要导出定制化的excel表格,其实主要还是通过table表格调整colspan和rowspan

问题:

因为要附带客户公司的集团logo图片,而原生的导出虽然能在第一次导出时导出图片,但是导出文件为xls文件,用wps打开时能看到图片,但是用office打开不可见,而且在保存后图片会消失。因为是xls文件

项目相关:.net项目,没有工程化

代码:

·

      let template = '';
        template += '<html lang="" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40">';
        template += '<head><meta charset="UTF-8"><!--[if gte mso 9]><title></title>';
        template += '<xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet>';
        template += '<x:Name>';
        template += '检验记录';
        template += '</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions>';
        template += '</x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml>';
        template += '</head>';
        template += '<body><table >';
        // template += $('#table-head').html()
        // <img style='margin-top:20px;margin-left:20px' src='data:image/jpg;base64,${base64}'         width=260 />
        template += `
             <thead style="width: 100%;">
              <tr>
              </tr>
            </thead>` //这里是需要导出的表格内容
        template += '</table><table id="print_table_1" class="print_table_1" border="1">';
        template += $('#print_table_1').html(); //可以将多个表格拼接
        template += '</table>';
        template += `<table>${$('#table-footer').html()}</table>` //可以将多个表格拼接
        template += '</body></html>';

        // 将html编码为excel文件,并下载
        let url = 'data:application/vnd.ms-excel;base64,' + window.btoa(unescape(encodeURIComponent(template)));
        // const blob = new Blob([template], { type: 'application/vnd.ms-excel' });
        // const url = URL.createObjectURL(blob);
        let a = document.createElement('a');
        a.href = url;
        a.download = '文件名称.xls';
        a.click();

还有许多问题,给自己埋个坑,提醒自己解决

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值