vue 使用excel 导出-携带图片可控图片大小

vue 使用excel 导出-携带图片可控图片大小,第1张

/ eslint-disable /

let idTmr;

const getExplorer = () => {

    let explorer = windownavigatoruserAgent;

    //ie

    if (explorerindexOf("MSIE") >= 0) {

        return 'ie';

    }

    //firefox

    else if (explorerindexOf("Firefox") >= 0) {

        return 'Firefox';

    }

    //Chrome

    else if (explorerindexOf("Chrome") >= 0) {

        return 'Chrome';

    }

    //Opera

    else if (explorerindexOf("Opera") >= 0) {

        return 'Opera';

    }

    //Safari

    else if (explorerindexOf("Safari") >= 0) {

        return 'Safari';

    }

}

// 判断浏览器是否为IE

const exportToExcel = (data, name) => {

    // 判断是否为IE

    if (getExplorer() == 'ie') {

        tableToIE(data, name)

    } else {

        tableToNotIE(data, name)

    }

}

const Cleanup = () => {

    windowclearInterval(idTmr);

}

// ie浏览器下执行

const tableToIE = (data, name) => {

    let curTbl = data;

    let oXL = new ActiveXObject("ExcelApplication");

    //创建AX对象excel

    let oWB = oXLWorkbooksAdd();

    //获取workbook对象

    let xlsheet = oWBWorksheets(1);

    //激活当前sheet

    let sel = documentbodycreateTextRange();

    selmoveToElementText(curTbl);

    //把表格中的内容移到TextRange中

    selselect;

    //全选TextRange中内容

    selexecCommand("Copy");

    //复制TextRange中内容

    xlsheetPaste();

    //粘贴到活动的EXCEL中

    oXLVisible = true;

    //设置excel可见属性

    try {

        let fname = oXLApplicationGetSaveAsFilename("Excelxls", "Excel Spreadsheets (xls), xls");

    } catch (e) {

        print("Nested catch caught " + e);

    } finally {

        oWBSaveAs(fname);

        oWBClose(savechanges = false);

        //xlsvisible = false;

        oXLQuit();

        oXL = null;

        // 结束excel进程,退出完成

        windowsetInterval("Cleanup();", 1);

        idTmr = windowsetInterval("Cleanup();", 1);

    }

}

// 非ie浏览器下执行

const tableToNotIE = (function () {

    // 编码要用utf-8不然默认gbk会出现中文乱码

    const uri = 'data:application/vndms-excel;base64,',

        template = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://wwww3org/TR/REC-html40"><head><meta 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>';

    const base64 = function (s) {

        return windowbtoa(unescape(encodeURIComponent(s)));

    }

    const format = (s, c) => {

        return sreplace(/{(\w+)}/g,

            (m, p) => {

                return c[p];

            })

    }

    return (table, name) => {

        const ctx = {

            worksheet: name,

            table

        }

        const url = uri + base64(format(template, ctx));

        if (navigatoruserAgentindexOf("Firefox") > -1){

            windowlocationhref = url

        } else {

            const aLink = documentcreateElement('a');

            aLinkhref = url;

            aLinkdownload = name || '';

            let event;

            if (windowMouseEvent) {

                event = new MouseEvent('click');

            } else {

                event = documentcreateEvent('MouseEvents');

                eventinitMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);

            }

            aLinkdispatchEvent(event);

        }

    }

})()

// 导出函数

const table2excel = (column, data, excelName) => {

    const typeMap = {

        image: getImageHtml,

        text: getTextHtml

    }

    let thead = columnreduce((result, item) => {

        result += `<th>${itemtitle}</th>`

        return result

    }, '')

    thead = `<thead><tr>${thead}</tr></thead>`

    let tbody = datareduce((result, row) => {

        const temp = columnreduce((tds, col) => {

            tds += typeMap[coltype || 'text'](row[colkey], col)

            return tds

        }, '')

        result += `<tr>${temp}</tr>`

        return result

    }, '')

    tbody = `<tbody>${tbody}</tbody>`

    const table = thead + tbody

    // 导出表格

    exportToExcel(table, excelName)

    function getTextHtml(val) {

        return `<td  style="text-align: center;vndms-excelnumberformat:@">${val}</td>`

    }

    function getImageHtml(val, options) {

        options = Objectassign({width: 40, height: 60}, options)

        return `<td style="width: ${optionswidth}px; height: ${optionsheight}px; text-align: center; vertical-align: middle"><img src="${val}" width=${optionswidth} height=${optionsheight}></td>`

    }

}

export default table2excel

以上就是关于vue 使用excel 导出-携带图片可控图片大小全部的内容,包括:vue 使用excel 导出-携带图片可控图片大小、、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/9521667.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-29
下一篇 2023-04-29

发表评论

登录后才能评论

评论列表(0条)

保存