jquery excel文件導(dǎo)出

1.需要jquery-3.3.1版本 可去官網(wǎng)下載

2.新建插件xlsx.full.min.js

3.

/*

*? jQuery table2excel - v1.1.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",

filename:"table2excel",

fileext:".xls",

exclude_img:true,

exclude_links:true,

exclude_inputs:true

? ? };

// 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;

var utf8Heading ="<meta http-equiv=\"content-type\" content=\"application/vnd.ms-excel; charset=UTF-8\">";

e.template = {

head:"<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\">" + utf8Heading +"<head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets>",

sheet: {

head:"<x:ExcelWorksheet><x:Name>",

tail:""

? ? ? ? ? ? ? ? },

mid:"</x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head><body>",

table: {

head:"<table>",

tail:""

? ? ? ? ? ? ? ? },

foot:""

? ? ? ? ? ? };

e.tableRows = [];

// get contents of table except for exclude

? ? ? ? ? ? $(e.element).each(function(i,o) {

var tempRows ="";

$(o).find("tr").not(e.settings.exclude).each(function (i,p) {

tempRows +="<tr>";

$(p).find("td,th").not(e.settings.exclude).each(function (i,q) {// p did not exist, I corrected

? ? ? ? ? ? ? ? ? ? ? ? var rc = {

rows: $(this).attr("rowspan"),

cols: $(this).attr("colspan"),

flag: $(q).find(e.settings.exclude)

};

if( rc.flag.length >0 ) {

tempRows +="<td> </td>";// exclude it!!

? ? ? ? ? ? ? ? ? ? ? ? }else {

if( rc.rows? & rc.cols ) {

tempRows +="<td>" + $(q).html() +"</td>";

}else {

tempRows +="<td";

if( rc.rows >0) {

tempRows +=" rowspan=\'" + rc.rows +"\' ";

}

if( rc.cols >0) {

tempRows +=" colspan=\'" + rc.cols +"\' ";

}

tempRows +="/>" + $(q).html() +"</td>";

}

}

});

tempRows +="</tr>";

});

// exclude img tags

? ? ? ? ? ? ? ? if(e.settings.exclude_img) {

tempRows = exclude_img(tempRows);

}

// exclude link tags

? ? ? ? ? ? ? ? if(e.settings.exclude_links) {

tempRows = exclude_links(tempRows);

}

// exclude input tags

? ? ? ? ? ? ? ? if(e.settings.exclude_inputs) {

tempRows = exclude_inputs(tempRows);

}

e.tableRows.push(tempRows);

});

e.tableToExcel(e.tableRows, e.settings.name, e.settings.sheetName);

},

tableToExcel:function (table, name, sheetName) {

var e =this, fullTemplate="", i, link, a;

e.format =function (s, c) {

return s.replace(/{(\w+)}/g,function (m, p) {

return c[p];

});

};

sheetName =typeof sheetName ==="undefined" ?"Sheet" : sheetName;

e.ctx = {

worksheet: name ||"Worksheet",

table: table,

sheetName: sheetName

};

fullTemplate= e.template.head;

if ( $.isArray(table) ) {

for (iin table) {

//fullTemplate += e.template.sheet.head + "{worksheet" + i + "}" + e.template.sheet.tail;

? ? ? ? ? ? ? ? ? ? ? fullTemplate += e.template.sheet.head + sheetName + i + e.template.sheet.tail;

}

}

fullTemplate += e.template.mid;

if ( $.isArray(table) ) {

for (iin table) {

fullTemplate += e.template.table.head +"{table" + i +"}" + e.template.table.tail;

}

}

fullTemplate += e.template.foot;

for (iin table) {

e.ctx["table" + i] = table[i];

}

delete e.ctx.table;

var isIE =/*@cc_on!@*/false || !!document.documentMode;// this works with IE10 and IE11 both :)

//if (typeof msie !== "undefined" && msie > 0 || !!navigator.userAgent.match(/Trident.*rv\:11\./))? ? ? // this works ONLY with IE 11!!!

? ? ? ? ? ? if (isIE) {

if (typeof Blob !=="undefined") {

//use blobs if we can

? ? ? ? ? ? ? ? ? ? fullTemplate = e.format(fullTemplate, e.ctx);// with this, works with IE

? ? ? ? ? ? ? ? ? ? fullTemplate = [fullTemplate];

//convert to array

? ? ? ? ? ? ? ? ? ? var blob1 =new Blob(fullTemplate, { type:"text/html" });

window.navigator.msSaveBlob(blob1, getFileName(e.settings)+".xls" );

}else {

//otherwise use the iframe and save

//requires a blank iframe on page called txtArea1

? ? ? ? ? ? ? ? ? ? txtArea1.document.open("text/html","replace");

txtArea1.document.write(e.format(fullTemplate, e.ctx));

txtArea1.document.close();

txtArea1.focus();

sa = txtArea1.document.execCommand("SaveAs",true, getFileName(e.settings) +".xls" );

}

}else {

var blob =new Blob([e.format(fullTemplate, e.ctx)], {type:"application/vnd.ms-excel"});

window.URL = window.URL || window.webkitURL;

link = window.URL.createObjectURL(blob);

a = document.createElement("a");

a.download = getFileName(e.settings) +".xls";

a.href = link;

document.body.appendChild(a);

a.click();

document.body.removeChild(a);

}

return true;

}

};

function getFileName(settings) {

return ( settings.filename ? settings.filename :"table2excel" );

}

// Removes all img tags

? ? function exclude_img(string) {

var _patt =/(\s+alt\s*=\s*"([^"]*)"|\s+alt\s*=\s*'([^']*)')/i;

return string.replace(/<img[^>]*>/gi,function myFunction(x){

var res = _patt.exec(x);

if (res !==null && res.length >=2) {

return res[2];

}else {

return "";

}

});

}

// Removes all link tags

? ? function exclude_links(string) {

return string.replace(/<a[^>]*>|<\/a>/gi,"");

}

// Removes input params

? ? function exclude_inputs(string) {

var _patt =/(\s+value\s*=\s*"([^"]*)"|\s+value\s*=\s*'([^']*)')/i;

return string.replace(/<input[^>]*>|<\/input>/gi,function myFunction(x){

var res = _patt.exec(x);

if (res !==null && res.length >=2) {

return res[2];

}else {

return "";

}

});

}

$.fn[ pluginName ] =function ( options ) {

var e =this;

e.each(function() {

if ( !$.data( e,"plugin_" + pluginName ) ) {

$.data( e,"plugin_" + pluginName,new Plugin(this, options ) );

}

});

// chain jQuery functions

? ? ? ? return e;

};

})( jQuery, window, document );

4.

5.<script language="JavaScript" type="text/javascript">

$(document).ready(function () {

$("#btnExport").click(function () {

$("#tb").table2excel({

exclude? :".noExl",//過(guò)濾位置的 css 類名

? ? ? ? ? ? ? ? filename :"河長(zhǎng)制項(xiàng)目進(jìn)展情況統(tǒng)計(jì)表",//文件名稱

? ? ? ? ? ? ? ? name:"Excel Document Name",

exclude_img:true,

exclude_links:true,

exclude_inputs:true

? ? ? ? ? ? });

});

});

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末呕缭,一起剝皮案震驚了整個(gè)濱河市洗出,隨后出現(xiàn)的幾起案子胖笛,更是在濱河造成了極大的恐慌纵顾,老刑警劉巖腌闯,帶你破解...
    沈念sama閱讀 217,542評(píng)論 6 504
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)署拟,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,822評(píng)論 3 394
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)歌豺,“玉大人推穷,你說(shuō)我怎么就攤上這事±噙郑” “怎么了馒铃?”我有些...
    開(kāi)封第一講書(shū)人閱讀 163,912評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)痕惋。 經(jīng)常有香客問(wèn)我区宇,道長(zhǎng),這世上最難降的妖魔是什么血巍? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,449評(píng)論 1 293
  • 正文 為了忘掉前任萧锉,我火速辦了婚禮珊随,結(jié)果婚禮上述寡,老公的妹妹穿的比我還像新娘柿隙。我一直安慰自己,他們只是感情好鲫凶,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,500評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布禀崖。 她就那樣靜靜地躺著,像睡著了一般螟炫。 火紅的嫁衣襯著肌膚如雪波附。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書(shū)人閱讀 51,370評(píng)論 1 302
  • 那天昼钻,我揣著相機(jī)與錄音掸屡,去河邊找鬼。 笑死然评,一個(gè)胖子當(dāng)著我的面吹牛仅财,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播碗淌,決...
    沈念sama閱讀 40,193評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼盏求,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了亿眠?” 一聲冷哼從身側(cè)響起碎罚,我...
    開(kāi)封第一講書(shū)人閱讀 39,074評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎纳像,沒(méi)想到半個(gè)月后荆烈,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,505評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡竟趾,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,722評(píng)論 3 335
  • 正文 我和宋清朗相戀三年耙考,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片潭兽。...
    茶點(diǎn)故事閱讀 39,841評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡倦始,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出山卦,到底是詐尸還是另有隱情鞋邑,我是刑警寧澤,帶...
    沈念sama閱讀 35,569評(píng)論 5 345
  • 正文 年R本政府宣布账蓉,位于F島的核電站枚碗,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏铸本。R本人自食惡果不足惜肮雨,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,168評(píng)論 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望箱玷。 院中可真熱鬧怨规,春花似錦陌宿、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,783評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至掰烟,卻和暖如春爽蝴,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背纫骑。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 32,918評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工蝎亚, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人先馆。 一個(gè)月前我還...
    沈念sama閱讀 47,962評(píng)論 2 370
  • 正文 我出身青樓颖对,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親磨隘。 傳聞我的和親對(duì)象是個(gè)殘疾皇子缤底,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,781評(píng)論 2 354