Javascript 表2 EXCEL trims前导零
我正在寻找一个插件复制表数据到excel文件。这里似乎有一些,但我很难找到一个不带数字字段前导零的 例如:序列号可能列为012005,但当导出时,零被修剪,并显示12005 代码如下:Javascript 表2 EXCEL trims前导零,javascript,jquery,Javascript,Jquery,我正在寻找一个插件复制表数据到excel文件。这里似乎有一些,但我很难找到一个不带数字字段前导零的 例如:序列号可能列为012005,但当导出时,零被修剪,并显示12005 代码如下: /* * jQuery table2excel - v1.0.2 * jQuery plugin to export an .xls file in browser from an HTML table * https://github.com/rainabba/jquery-table2e
/*
* jQuery table2excel - v1.0.2
* 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;
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: "</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet>"
},
mid: "</x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head><body>",
table: {
head: "<table>",
tail: "</table>"
},
foot: "</body></html>"
};
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 flag = $(q)
.find(e.settings.exclude); // does this <td> have something with an exclude class
if(flag.length >= 1) {
tempRows += "<td> </td>"; // exclude it!!
} else {
tempRows += "<td>" + $(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 (i in 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 (i in table) {
fullTemplate += e.template.table.head + "{table" + i + "}" + e.template.table.tail;
}
}
fullTemplate += e.template.foot;
for (i in 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) );
} 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) );
}
} 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);
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 );
/*
*jQuery table2excel-v1.0.2
*jQuery插件,用于在浏览器中从HTML表导出.xls文件
* https://github.com/rainabba/jquery-table2excel
*
*雷纳巴制作
*在麻省理工学院的许可下
*/
//表2EXCEL.js
;(函数($,窗口,文档,未定义){
var pluginName=“table2excel”,
默认值={
排除:“.noExl”,
名称:“Table2Excel”
};
//实际的插件构造函数
函数插件(元素、选项){
this.element=元素;
//jQuery有一个extend方法,它合并两个或多个数据库的内容
//更多对象,将结果存储在第一个对象中。第一个对象
//通常为空,因为我们不想更改的默认选项
//插件的未来实例
//
this.settings=$.extend({},默认值,选项);
这是默认值;
这个。_name=pluginName;
this.init();
}
Plugin.prototype={
init:函数(){
var e=此;
var utf8Heading=“”;
e、 模板={
标题:“+utf8Heading+”,
表:{
头:“,
尾巴:“
},
脚:“
};
e、 tableRows=[];
//获取表的内容(排除除外)
$(e.element)。每个(函数(i,o){
var tempRows=“”;
$(o).find(“tr”).not(e.settings.exclude).each(函数(i,p){
tempRows+=“”;
$(p).find(“td,th”).not(e.settings.exclude).each(函数(i,q){//p不存在,我更正了
var标志=$(q)
.find(e.settings.exclude);//这是否有包含exclude类的内容
如果(flag.length>=1){
tempRows+=“”;//排除它!!
}否则{
tempRows+=“”+$(q).html()+“”;
}
});
tempRows+=“”;
});
//排除img标签
如果(如设置排除){
tempRows=排除(tempRows);
}
//排除链接标记
如果(如设置、排除链接){
tempRows=排除链接(tempRows);
}
//排除输入标记
如果(如设置。排除输入){
tempRows=排除_输入(tempRows);
}
e、 tableRows.push(tempRows);
});
e、 tableToExcel(e.tableRows、e.settings.name、e.settings.sheetName);
},
tableToExcel:函数(表、名称、图纸名称){
var e=此,fullTemplate=“”,i,link,a;
e、 格式=函数(s,c){
返回s.replace(/{(\w+)}/g,函数(m,p){
返回c[p];
});
};
sheetName=typeof sheetName==“未定义”?“工作表”:工作表名称;
e、 ctx={
工作表:名称| |“工作表”,
表:表,,
sheetName:sheetName
};
fullTemplate=e.template.head;
如果($.isArray(表)){
对于(表中的i){
//fullTemplate+=e.template.sheet.head+“{sheet”+i+“}”+e.template.sheet.tail;
fullTemplate+=e.template.sheet.head+sheetName+i+e.template.sheet.tail;
}
}
fullTemplate+=e.template.mid;
如果($.isArray(表)){
对于(表中的i){
fullTemplate+=e.template.table.head+“{table”+i+“}”+e.template.table.tail;
}
}
fullTemplate+=e.template.foot;
对于(表中的i){
e、 ctx[“表”+i]=表[i];
}
删除e.ctx.table;
var isIE=/*@cc|u on!@*/false| |!!document.documentMode;//这适用于IE10和IE11:)
//if(typeof msie!=“undefined”&&msie>0 | | |!!navigator.userAgent.match(/Trident.*rv \:11\。/)//这仅适用于IE 11!!!
如果(isIE){
如果(Blob的类型!=“未定义”){
//如果可以的话,使用blob
fullTemplate=e.format(fullTemplate,e.ctx);//使用它,可以与IE一起工作
fullTemplate=[fullTemplate];
//转换为数组
var blob1=new Blob(fullTemplate,{type:“text/html”});
msSaveBlob(blob1,getFileName(e.settings));
}否则{
//否则,请使用iframe并保存
//需要名为txtArea1的页面上的空白iframe
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));
}
}否则{
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、 下载=获取文件名(如设置);
a、 href=链接;
文件.正文.附件(a);
a、 单击();
文件.body.removeChild(a);
}
返回true;
}
};
函数getFileName(设置){
返回(settings.filename?settings.filename:“table2excel”)