博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
js 前端 table 导出 excel
阅读量:4660 次
发布时间:2019-06-09

本文共 4921 字,大约阅读时间需要 16 分钟。

  园子,github,stackoverflow 关于前端下载的文章不少

  园子里大部分都是 利用ActiveXObject对象来实现,可他有个缺点安全等级,还有必须安装excel……

  github,stackoverflow  有点高大上了,几乎全是英文……无奈只能看看代码了,还好找到了一个比较好的方法

  直接上代码:还是看原文好  

    https://github.com/rainabba/jquery-table2excel 

    http://stackoverflow.com/questions/17126453/html-table-to-excel-javascript 

  当然一个需要引入jquery库,可也以自己去修改……

  html table内容

This shouldn't get exported This shouldn't get exported either
This Should get exported as a header This should too
data1a data1b
data2a data2b
This footer spans 2 cells

<!-- 方法1-->

<button οnclick="xiazai();">hello</button>

 
function xiazai() {            $("#table2excel").table2excel({                exclude: ".noExl",                name: "Excel Document Name"            });        }

 

  

这是方法一要引入的  tabletoexcle.js 源码

/* *  jQuery table2excel - v1.0.1 *  jQuery plugin to export an .xls file in browser from an HTML table *  https://github.com/rainabba/jquery-table2excel * *  Made by rainabba *  Under MIT License *///table2excel.js ; (function ($, window, document, undefined) {    var pluginName = "table2excel",                 defaults = {                     exclude: ".noExl",                     name: "Table2Excel"                 };    // The actual plugin constructor     function Plugin(element, options) {        this.element = element;        // jQuery has an extend method which merges the contents of two or         // more objects, storing the result in the first object. The first object         // is generally empty as we don't want to alter the default options for         // future instances of the plugin         this.settings = $.extend({}, defaults, options);        this._defaults = defaults;        this._name = pluginName;        this.init();    }    Plugin.prototype = {        init: function () {            var e = this;            e.template = "
{table}
"; e.tableRows = ""; // get contents of table except for exclude $(e.element).find("tr").not(this.settings.exclude).each(function (i, o) { e.tableRows += "" + $(o).html() + ""; }); this.tableToExcel(this.tableRows, this.settings.name); }, tableToExcel: function (table, name) { var e = this; e.uri = "data:application/vnd.ms-excel;base64,"; e.base64 = function (s) { return window.btoa(unescape(encodeURIComponent(s))); }; e.format = function (s, c) { return s.replace(/{(\w+)}/g, function (m, p) { return c[p]; }); }; e.ctx = { worksheet: name || "Worksheet", table: table }; window.location.href = e.uri + e.base64(e.format(e.template, e.ctx)); } }; $.fn[pluginName] = function (options) { this.each(function () { if (!$.data(this, "plugin_" + pluginName)) { $.data(this, "plugin_" + pluginName, new Plugin(this, options)); } }); // chain jQuery functions return this; };})(jQuery, window, document);

方法一有个缺点就是不知道怎么去命名文件

 

 

方法二呢可以明明文件喽

var tableToExcel = (function () {            var uri = 'data:application/vnd.ms-excel;base64,',            template = '
{table}
', 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, filename) { if (!table.nodeType) table = document.getElementById(table) var ctx = { worksheet: name || 'Worksheet', table: table.innerHTML } document.getElementById("dlink").href = uri + base64(format(template, ctx)); document.getElementById("dlink").download = filename; document.getElementById("dlink").click(); } })()

 

转载于:https://www.cnblogs.com/lengp/p/4238498.html

你可能感兴趣的文章
lambda表达式树
查看>>
二次注入原理及防御
查看>>
会话记住已登录功能
查看>>
Linux内核分析——可执行程序的装载
查看>>
第一阶段冲刺3
查看>>
父类引用指向子类对象
查看>>
网页如何实现下载功能
查看>>
IT男专用表白程序
查看>>
读《大道至简》第六章感想
查看>>
ef linq 中判断实体中是否包含某集合
查看>>
章三 链表
查看>>
Solution for Concurrent number of AOS' for this application exceeds the licensed number
查看>>
CSE 3100 Systems Programming
查看>>
IntelliJ IDEA 的Project structure说明
查看>>
Java Security(JCE基本概念)
查看>>
创建 PSO
查看>>
JasperReport报表设计4
查看>>
项目活动定义 概述
查看>>
团队冲刺04
查看>>
泛型在三层中的应用
查看>>