我在导出具有大量行数的HTML表格时遇到问题。正如我在标题中指出的那样,它有3000多行和大约6列。
我试过这个,How to export html table to excel using javascript
工作正常!好吧,至少在小桌子上。但是当我把它与我想要转换的表格绑定时,它会加载一段时间然后停止。
任何人都可以帮助我吗?
PS,我不想使用ActiveXObject,因为我不想仅限于使用IE。
提前致谢!
编辑:代码
var tableToExcel = (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><!--[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]--></head><body><table>{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}
window.location.href = uri + base64(format(template, ctx))
}
})()
适用于IE和Firefox,但不适用于Chrome。那是为什么?
答案 0 :(得分:2)
我在此处查看了答案表格,其中包含:how to export table as excel with 10000 to 40000 rows
它可以在Chrome上运行(尚未在IE上进行测试),可以将3867行保存到.xls文件;
希望这可以帮助某人。代码:
<input type="button" class="col-2 btn btn-info" onclick="tableToExcel(myTableId)" value="Excel">
function tableToExcel(tableId) {
let 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="https://www.w3.org/TR/html401/"><head><!--[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]--><meta http-equiv="content-type" content="text/plain; charset=UTF-8"/></head><body><table>{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]; })}
let table = document.getElementById(tableId)
if (!table) {
return;
}
let ctx = {worksheet: tableId || 'Worksheet', table: table.innerHTML}
var str = base64(format(template, ctx));
var blob = b64toBlob(str, "application/vnd.ms-excel");
var blobUrl = URL.createObjectURL(blob);
let link = document.createElement('a');
link.download = openedTabId + '.xls'; // the fileName for download
link.href = blobUrl;
link.click();
// window.location = blobUrl; // instead of using a link, could also do this;
function b64toBlob(b64Data, contentType='', sliceSize=512) {
var byteCharacters = atob(b64Data);
var byteArrays = [];
for (var offset = 0; offset < byteCharacters.length; offset += sliceSize) {
var slice = byteCharacters.slice(offset, offset + sliceSize);
var byteNumbers = new Array(slice.length);
for (var i = 0; i < slice.length; i++) {
byteNumbers[i] = slice.charCodeAt(i);
}
var byteArray = new Uint8Array(byteNumbers);
byteArrays.push(byteArray);
}
var blob = new Blob(byteArrays, {type: contentType});
return blob;
}
}
答案 1 :(得分:0)
下面的代码适用于我的chrome表,超过2000条记录。
csv = []
rows = $('#YourHTMLtableId tr');
for(i =0;i < rows.length;i++) {
cells = $(rows[i]).find('td,th');
csv_row = [];
for (j=0;j<cells.length;j++) {
txt = cells[j].innerText;
csv_row.push(txt.replace(",", "-"));
}
csv.push(csv_row.join(","));
}
output = csv.join("\n")
var textToSave = output;
var textToSaveAsBlob = new Blob([textToSave], {type:"text/plain"});
var textToSaveAsURL = window.URL.createObjectURL(textToSaveAsBlob);
var fileNameToSaveAs = "something.csv";
var downloadLink = document.createElement("a");
downloadLink.download = fileNameToSaveAs;
downloadLink.innerHTML = "Download File";
downloadLink.href = textToSaveAsURL;
downloadLink.onclick = destroyClickedElement;
function destroyClickedElement(event)
{
document.body.removeChild(event.target);
}
downloadLink.style.display = "none";
document.body.appendChild(downloadLink);
downloadLink.click();