记一次接私活 -- 油猴脚本 -- 导出在线表格

GoogleVip8 1年前 ⋅ 2029 阅读

需求方:银行职员 需求:下载银行内部网络访问的在线表格 成交价:300人民币 研发时间:4小时 研发思路:增加可点击按钮,获取网页上的table,格式化为utf-8,转为base64,导出excel兼容模式; 最终效果:点击右下角图片下载网页上的table 084229F0-5D54-4F78-8C69-00401E7C6650.png

// ==UserScript==
// @name         DownLoadExcel
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  try to take over the world!
// @author       MT-牧头
// @match        *://*/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // Your code here...
    	// document.body.innerHTML = "<div id=\"neko\"></div>";
	var wrap=document.createElement("div");
    wrap.setAttribute('id','neko');
	var first=document.body.firstChild;//得到页面的第一个元素
	var wraphtml=document.body.insertBefore(wrap,first);

	var neko = document.querySelector('#neko');
	neko.style.margin = '0';
    neko.style.zIndex ='99999';
	neko.style.padding = '0';
	neko.style.listStyle = 'none';
	neko.style.width = '100px';
	neko.style.height = '100px';
	neko.style.background = '#ddd';
	neko.style.position = 'fixed';
	neko.style.cursor = 'move';
	neko.setAttribute('box-sizing','border-box');
	neko.style.border = '4px solid #66cc66';
	neko.style.borderRadius = '50%';
	//neko.style.background = 'url(\'https://graph.baidu.com/thumb/3692891163,927466470.jpg\') no-repeat center center';
	neko.style.backgroundSize = '100% 100%';
	neko.style.overflow = 'hidden';
    var nekoW = neko.offsetWidth;
	var nekoH = neko.offsetHeight;
	var cuntW = 0;
	var cuntH = 0;
	neko.style.left = parseInt(document.body.offsetWidth *0.95) + 'px';
	neko.style.top = parseInt(document.body.offsetHeight *0.9) + 'px';
    neko.onclick = function(){
		HtmlExportToExcel("m_excelWebRenderer_ewaCtl_scrollTable","下载的excel");
	}
    function HtmlExportToExcel(tableid,filename) {
        if (getExplorer() == 'ie' || getExplorer() == undefined) {
            HtmlExportToExcelForIE(tableid, filename);
        }
        else {
            HtmlExportToExcelForEntire(tableid, filename)
        }
    }
    //IE浏览器导出Excel
    function HtmlExportToExcelForIE(tableid, filename) {
        try {
            var winname = window.open('', '_blank', 'top=10000');
            var strHTML = document.getElementById(tableid).innerHTML;

            winname.document.open('application/vnd.ms-excel', 'export excel');
            winname.document.writeln(strHTML);
            winname.document.execCommand('saveas', '', filename + '.xls');
            winname.close();

        } catch (e) {
            alert(e.description);
        }
    }
    //非IE浏览器导出Excel
    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 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';
        }
	}

    function move(obj, w, h) {
		if (obj.direction === 'left') {
			obj.style.left = 0 - w + 'px';
		} else if (obj.direction === 'right') {

			obj.style.left = document.body.offsetWidth - nekoW + w + 'px';
		}
		if (obj.direction === 'top') {
			obj.style.top = 0 - h + 'px';
		} else if (obj.direction === 'bottom') {
			obj.style.top = document.body.offsetHeight - nekoH + h + 'px';
		}
	}

	function rate(obj, a) {
		//  console.log(a);
		obj.style.transform = ' rotate(' + a + ')'
	}

	function action(obj) {

		var dir = obj.direction;

		switch (dir) {
			case 'left':
				rate(obj, '90deg');
				break;
			case 'right':
				rate(obj, '-90deg');
				break;
			case 'top':
				rate(obj, '-180deg');
				break;
			default:
				rate(obj, '-0');
				break;
		}

	}
	neko.onmousedown = function (e) {
		var nekoL = e.clientX - neko.offsetLeft;
		var nekoT = e.clientY - neko.offsetTop;
		document.onmousemove = function (e) {
			cuntW = 0;
			cuntH = 0;
			neko.direction = '';
			neko.style.transition = '';
			neko.style.left = (e.clientX - nekoL) + 'px';
			neko.style.top = (e.clientY - nekoT) + 'px';
			if (e.clientX - nekoL < 5) {
				neko.direction = 'left';
			}
			if (e.clientY - nekoT < 5) {
				neko.direction = 'top';
			}
			if (e.clientX - nekoL > document.body.offsetWidth - nekoW - 5) {
				neko.direction = 'right';
			}
			if (e.clientY - nekoT > document.body.offsetHeight - nekoH - 5) {
				neko.direction = 'bottom';
			}

			move(neko, 0, 0);


		}
	}
	neko.onmouseover = function () {
		move(this, 0, 0);
		rate(this, 0)
	}

	neko.onmouseout = function () {
		move(this, nekoW / 2, nekoH / 2);
		action(this);
	}

	neko.onmouseup = function () {
		document.onmousemove = null;
		this.style.transition = '.5s';
		move(this, nekoW / 2, nekoH / 2);
		action(this);
	}

	window.onresize = function () {
		var bodyH = document.body.offsetHeight;
		var nekoT = neko.offsetTop;
		var bodyW = document.body.offsetWidth;
		var nekoL = neko.offsetLeft;

		if (nekoT + nekoH > bodyH) {
			neko.style.top = bodyH - nekoH + 'px';
			cuntH++;
		}
		if (bodyH > nekoT && cuntH > 0) {
			neko.style.top = bodyH - nekoH + 'px';
		}
		if (nekoL + nekoW > bodyW) {
			neko.style.left = bodyW - nekoW + 'px';
			cuntW++;
		}
		if (bodyW > nekoL && cuntW > 0) {
			neko.style.left = bodyW - nekoW + 'px';
		}

		move(neko, nekoW / 2, nekoH / 2);
	}

})();

全部评论: 0

    我有话说: