Javascript 将html5表导出到excel jquery
我想用js或jquery将我的html表格导出到excel表格。我在google上搜索过,但没有得到任何有用的资源。这是我的html表格代码Javascript 将html5表导出到excel jquery,javascript,jquery,html,excel,Javascript,Jquery,Html,Excel,我想用js或jquery将我的html表格导出到excel表格。我在google上搜索过,但没有得到任何有用的资源。这是我的html表格代码 <table> <thead id='headers'> <tr> <th>Select</th> <th>Name</th> <t
<table>
<thead id='headers'>
<tr>
<th>Select</th>
<th>Name</th>
<th>Mobile</th>
<th>Mail ID</th>
<th>Rating</th>
<th>Date</th>
<th>Notify</th>
<th>View</th>
</tr>
</thead>
<tbody>
<tr>
<td><input type="checkbox"/></td>
<td>Praveen</td>
<td>97910123123</td>
<td>praveen@360i</td>
<td>5 star</td>
<td>15.2.2013</td>
<td>A</td>
<td>4</td>
</tr>
<tr>
<td><input type="checkbox"/></td>
<td>kumar</td>
<td>97912342333</td>
<td>kumar@360i</td>
<td>4 star</td>
<td>16.2.2013</td>
<td>D</td>
<td>3</td>
</tr>
</tbody>
</table>
挑选
名称
可移动的
邮件ID
评级
日期
通知
看法
普拉文
97910123123
praveen@360i
五星
15.2.2013
A.
4.
库玛
97912342333
kumar@360i
四星
16.2.2013
D
3.
请帮我找出任何解决方案………这是解决方案()
这是解决方案()
我发现,它可以在Chrome(63)、Firefox(57)和IE11中使用。简而言之,您可以通过切片和连接表行来创建csv。接下来创建一个类型为“text/csv”的Blob,最后使用锚定“a”标记的下载属性下载它,这样页面就不会导航到该文件
var xport = {
_fallbacktoCSV: true,
toXLS: function(tableId, filename) {
this._filename = (typeof filename == 'undefined') ? tableId : filename;
//var ieVersion = this._getMsieVersion();
//Fallback to CSV for IE & Edge
if ((this._getMsieVersion() || this._isFirefox()) && this._fallbacktoCSV) {
return this.toCSV(tableId);
} else if (this._getMsieVersion() || this._isFirefox()) {
alert("Not supported browser");
}
//Other Browser can download xls
var htmltable = document.getElementById(tableId);
var html = htmltable.outerHTML;
this._downloadAnchor("data:application/vnd.ms-excel" + encodeURIComponent(html), 'xls');
},
toCSV: function(tableId, filename) {
this._filename = (typeof filename === 'undefined') ? tableId : filename;
// Generate our CSV string from out HTML Table
var csv = this._tableToCSV(document.getElementById(tableId));
// Create a CSV Blob
var blob = new Blob([csv], { type: "text/csv" });
// Determine which approach to take for the download
if (navigator.msSaveOrOpenBlob) {
// Works for Internet Explorer and Microsoft Edge
navigator.msSaveOrOpenBlob(blob, this._filename + ".csv");
} else {
this._downloadAnchor(URL.createObjectURL(blob), 'csv');
}
},
_getMsieVersion: function() {
var ua = window.navigator.userAgent;
var msie = ua.indexOf("MSIE ");
if (msie > 0) {
// IE 10 or older => return version number
return parseInt(ua.substring(msie + 5, ua.indexOf(".", msie)), 10);
}
var trident = ua.indexOf("Trident/");
if (trident > 0) {
// IE 11 => return version number
var rv = ua.indexOf("rv:");
return parseInt(ua.substring(rv + 3, ua.indexOf(".", rv)), 10);
}
var edge = ua.indexOf("Edge/");
if (edge > 0) {
// Edge (IE 12+) => return version number
return parseInt(ua.substring(edge + 5, ua.indexOf(".", edge)), 10);
}
// other browser
return false;
},
_isFirefox: function(){
if (navigator.userAgent.indexOf("Firefox") > 0) {
return 1;
}
return 0;
},
_downloadAnchor: function(content, ext) {
var anchor = document.createElement("a");
anchor.style = "display:none !important";
anchor.id = "downloadanchor";
document.body.appendChild(anchor);
// If the [download] attribute is supported, try to use it
if ("download" in anchor) {
anchor.download = this._filename + "." + ext;
}
anchor.href = content;
anchor.click();
anchor.remove();
},
_tableToCSV: function(table) {
// We'll be co-opting `slice` to create arrays
var slice = Array.prototype.slice;
return slice
.call(table.rows)
.map(function(row) {
return slice
.call(row.cells)
.map(function(cell) {
return '"t"'.replace("t", cell.textContent);
})
.join(",");
})
.join("\r\n");
}
};
我发现,它可以在Chrome(63)、Firefox(57)和IE11中使用。简而言之,您可以通过切片和连接表行来创建csv。接下来创建一个类型为“text/csv”的Blob,最后使用锚定“a”标记的下载属性下载它,这样页面就不会导航到该文件
var xport = {
_fallbacktoCSV: true,
toXLS: function(tableId, filename) {
this._filename = (typeof filename == 'undefined') ? tableId : filename;
//var ieVersion = this._getMsieVersion();
//Fallback to CSV for IE & Edge
if ((this._getMsieVersion() || this._isFirefox()) && this._fallbacktoCSV) {
return this.toCSV(tableId);
} else if (this._getMsieVersion() || this._isFirefox()) {
alert("Not supported browser");
}
//Other Browser can download xls
var htmltable = document.getElementById(tableId);
var html = htmltable.outerHTML;
this._downloadAnchor("data:application/vnd.ms-excel" + encodeURIComponent(html), 'xls');
},
toCSV: function(tableId, filename) {
this._filename = (typeof filename === 'undefined') ? tableId : filename;
// Generate our CSV string from out HTML Table
var csv = this._tableToCSV(document.getElementById(tableId));
// Create a CSV Blob
var blob = new Blob([csv], { type: "text/csv" });
// Determine which approach to take for the download
if (navigator.msSaveOrOpenBlob) {
// Works for Internet Explorer and Microsoft Edge
navigator.msSaveOrOpenBlob(blob, this._filename + ".csv");
} else {
this._downloadAnchor(URL.createObjectURL(blob), 'csv');
}
},
_getMsieVersion: function() {
var ua = window.navigator.userAgent;
var msie = ua.indexOf("MSIE ");
if (msie > 0) {
// IE 10 or older => return version number
return parseInt(ua.substring(msie + 5, ua.indexOf(".", msie)), 10);
}
var trident = ua.indexOf("Trident/");
if (trident > 0) {
// IE 11 => return version number
var rv = ua.indexOf("rv:");
return parseInt(ua.substring(rv + 3, ua.indexOf(".", rv)), 10);
}
var edge = ua.indexOf("Edge/");
if (edge > 0) {
// Edge (IE 12+) => return version number
return parseInt(ua.substring(edge + 5, ua.indexOf(".", edge)), 10);
}
// other browser
return false;
},
_isFirefox: function(){
if (navigator.userAgent.indexOf("Firefox") > 0) {
return 1;
}
return 0;
},
_downloadAnchor: function(content, ext) {
var anchor = document.createElement("a");
anchor.style = "display:none !important";
anchor.id = "downloadanchor";
document.body.appendChild(anchor);
// If the [download] attribute is supported, try to use it
if ("download" in anchor) {
anchor.download = this._filename + "." + ext;
}
anchor.href = content;
anchor.click();
anchor.remove();
},
_tableToCSV: function(table) {
// We'll be co-opting `slice` to create arrays
var slice = Array.prototype.slice;
return slice
.call(table.rows)
.map(function(row) {
return slice
.call(row.cells)
.map(function(cell) {
return '"t"'.replace("t", cell.textContent);
})
.join(",");
})
.join("\r\n");
}
};
你确定哪里没有有用的结果吗?这里没有ie解决方案。。通过使用(COLUMN name)和(rows)标记内容到Excel,您想要一个跨浏览器解决方案吗?另外,您想要IE11还是Edge就足够了?您确定哪里没有有用的结果吗?这里没有ie解决方案。。通过使用(column name)和(rows)标记内容到Excel,您想要跨浏览器解决方案吗?另外,您想要IE11还是Edge就足够了?