js-export2Excel.js-带图片excel生成器

50 篇文章 1 订阅

js-export2Excel.js-带图片excel生成器 目录


前言

不足与改进方向

  1. 无法指定下载文件的名字
  2. 对内容的修改灵活性不足
  3. office excelxml了解不够深入

生成效果展示

在这里插入图片描述

模块 - export2Excel_module.js

// IE Type
const BasicConf = {
    Type: [
        "MSIE",
        "Firefox",
        "Chrome",
        "Opera",
        "Safari"
    ],
    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>
                    <meta charset="UTF-8">
                    <title>{title}</title>
                    <!--[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>`,
    RegexpString: /http/,
};

let export2Excel = function(tHeadData, tBodyData, dataName, picWidth, picHeight) {
    this.explorer;
    this.idTmr;
    this.base64;
    this.format;
    this.excute(tHeadData, tBodyData, dataName, picWidth, picHeight);
}
export2Excel.prototype = {
    CleanUp() {
        window.clearInterval(this.idTmr);
    },

    GetExplorerType() {
        const explorer = window.navigator.userAgent;
        for(let i of BasicConf.Type){
            // console.log(i);
            if(explorer.indexOf(i) >= 0 ) {
                this.explorer = i;
                return;
            }
        }
    },

    JudgeType(data, name) {
        this.explorer == 'ie' ? this.tableToIE(data,name) : this.tableToNotIE(data,name);
        return;
    },

    tableToIE(data, name) {
        let curTbl = data,
            oXL = new ActiveXObject("Excel.Application");

        //创建AX对象excel
        let oWB = oXL.Workbooks.Add();
        //获取workbook对象
        let xlsheet = oWB.Worksheets(1);
        //激活当前sheet
        let sel = document.body.createTextRange();
        sel.moveToElementText(curTbl);
        //把表格中的内容移到TextRange中
        sel.select;
        //全选TextRange中内容
        sel.execCommand("Copy");
        //复制TextRange中内容
        xlsheet.Paste();
        //粘贴到活动的EXCEL中
        
        oXL.Visible = true;
        //设置excel可见属性

        try{
            let fname = oXL.Application.GetSaveAsFilename("Excel.xlsx", "Excel Spreadsheets (*.xls), *.xls");
        }catch(err) {
            console.log(`Nested catch caught: ${err}`);
        } finally {
            oWB.SaveAs(fname);
 
            oWB.Close(savechanges = false);
            //xls.visible = false;
            oXL.Quit();
            oXL = null;
            // 结束excel进程,退出完成
            window.setInterval(this.Cleanup, 1);
            this.idTmr = window.setInterval(this.CleanUp, 1);
        }
    },

    excute(tHeadData, tBodyData, dataName, picWidth, picHeight) {
        const re = BasicConf.RegexpString,
            th_len = tHeadData.length,
            tb_len = tBodyData.length;

        const width = picWidth,
            height = picHeight;

        // 添加表头信息
        let thead = '<thead><tr>';

        for (let i = 0; i < th_len; i++) {
            thead += '<th>' + tHeadData[i] + '</th>';
        }

        thead += '</tr></thead>';

        // 添加每一行数据
        let tbody = '<tbody>';

        for (let i = 0; i < tb_len; i++) {
            tbody += '<tr>';
            let row = tBodyData[i]; // 获取每一行数据
    
            for (let key in row) {
                if (re.test(row[key])) { // 如果为图片,则需要加div包住图片
                 
                    tbody += '<td style="width:' + width + 'px; height:' + height + 'px; text-align: center; vertical-align: middle"><div style="display:inline"><img src=\'' + row[key] + '\' ' + ' ' + 'width=' + '\"' + width + '\"' + ' ' + 'height=' + '\"' + height + '\"' + '></div></td>';
                } else {
                    tbody += '<td style="text-align:center">' + row[key] + '</td>';
                }
            }
            
            tbody += '</tr>';
        }
        
        tbody += '</tbody>';
    
        let table = thead + tbody;

        // export
        this.JudgeType(table, dataName);
        // console.log(dataName);
    }

}

export2Excel.prototype.tableToNotIE = (function() {
        
    const uri = BasicConf.URI;
    const template = BasicConf.template;

    this.base64 = (event) => window.btoa(unescape(encodeURIComponent(event)));
    this.format = (str, c) => {
        // console.log( str.replace(/{(\w+)}/g, (m,p)=>console.log(p)));
        return str.replace(/{(\w+)}/g,
                (m, p) => {
                    console.log(c[p]);
                    return c[p];
                });
    }

    return (table, name) => {
        const ctx = {
            title: 'fwx',
            worksheet: name,
            table
        };
        

        let link = document.createElement('a');
        
        link.setAttribute('href', 
            uri + this.base64(
                this.format(template,ctx)
            )
        );
        // console.log(link);
        link.click();
    }

})();

使用代码

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <!-- <script src="./Excel-/Export2Excel.js"></script> -->
    <!-- <script src="./Excel-/Blob.js"></script> -->
    <script src="./export2Excel_module.js"></script>
    <script src="./main.js"></script>
</body>
</html>

main.js

// main.js
// pic
const conf = {
    pic: "https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=2801998497,4036145562&fm=27&gp=0.jpg",
    picWidth: 40,
    picHeight: 60,
    excelWorkBookName: 'test'
};
// header, body
const tHeader = [
    'flower',
    'color',
    'pic'
];

const tBody = [
    {
        name: 'rose',
        color: 'red',
        pic: conf.pic
    },
    {
        name: 'rose2',
        color: 'red',
        pic: conf.pic
    },
    {
        name: 'rose3',
        color: 'red',
        pic: conf.pic
    }
];



let test = new export2Excel(tHeader, tBody, conf.excelWorkBookName, conf.picWidth, conf.picHeight);
// export2Excel(tHeader, tBody, 'test')
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值