save table

help someone save html table to xls easy~

目前为 2022-08-26 提交的版本。查看 最新版本

// ==UserScript==
// @name         save table
// @namespace    http://tampermonkey.net/
// @version      0.1
// @match        http://quote.eastmoney.com/center/*
// @description  help someone save html table to xls easy~
// @author       vector
// @grant        none
// @license MIT
// ==/UserScript==
(function () {


    function HtmlExportToExcel(tableid, filename) {
        if (getExplorer() === 'ie' || getExplorer() === undefined) {
            HtmlExportToExcelForIE(tableid, filename);
        } else {
            HtmlExportToExcelForEntire(tableid, filename)
        }
    }

    function getExplorer() {
        var explorer = window.navigator.userAgent;
        //ie
        if (explorer.indexOf("MSIE") >= 0) {
            return 'ie';
        }
        //firefox
        else if (explorer.indexOf("Firefox") >= 0) {
            return 'Firefox';
        }
        //Chrome
        else if (explorer.indexOf("Chrome") >= 0) {
            return 'Chrome';
        }
        //Opera
        else if (explorer.indexOf("Opera") >= 0) {
            return 'Opera';
        }
        //Safari
        else if (explorer.indexOf("Safari") >= 0) {
            return 'Safari';
        }
    }

    var HtmlExportToExcelForEntire = (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><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]--><style>table td {font-size: 12px;width: 200px;height: 30px;text-align: center;border:1px 1px;}</style></head><body><table border="1">{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 a = document.createElement("a");
            a.href = uri + base64(format(template, ctx));
            a.download = name + ".xls";
            a.click();
        }
    })()

    function genBtn() {
        var btn = document.createElement('span');
        btn.style = 'margin: 0 8px;color:blue;cursor:pointer;';
        btn.innerText = '保存';
        btn.onclick = function () {
            // 下载整个表格
            HtmlExportToExcel('table_wrapper-table', guid());
        };
        return btn;
    }

    function createDownloadBtn() {
        var element = document.querySelector(`#navbar`);
        element?.appendChild(genBtn());
    }
    function guid() {
        return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
            var r = Math.random() * 16 | 0,
                v = c == 'x' ? r : (r & 0x3 | 0x8);
            return v.toString(16);
        });
    }


    createDownloadBtn();
})();