【发布时间】:2020-10-02 19:18:19
【问题描述】:
我发现这个函数用于导出 HTML 表格以便从 this article 下载 excel。我正在使用cheerio.js 抓取网络数据并将其放入MongoDB 集合中,然后将其显示在表格中。该函数似乎一直有效,直到它在其中一个单元格中到达“#”,然后停止导出。
由于我没有自己编写函数,我不确定是什么原因造成的,但我有一种感觉,如果我添加或更改一些正则表达式会有所帮助吗?我自己从未使用过正则表达式,所以我真的不确定。
如果有人认为这会有所帮助,我可以发布我的网页抓取/html 表格代码。
HTML 到表格导出:
function exportTableToExcel(tableID, filename = ''){
var downloadLink;
var dataType = 'application/vnd.ms-excel';
var tableSelect = document.getElementById(tableID);
var tableHTML = tableSelect.outerHTML.replace(/ /g, '%20');
// Specify file name
filename = filename?filename+'.xls':'excel_data.xls';
// Create download link element
downloadLink = document.createElement("a");
document.body.appendChild(downloadLink);
if(navigator.msSaveOrOpenBlob){
var blob = new Blob(['\ufeff', tableHTML], {
type: dataType
});
navigator.msSaveOrOpenBlob( blob, filename);
}else{
// Create a link to the file
downloadLink.href = 'data:' + dataType + ', ' + tableHTML;
// Setting the file name
downloadLink.download = filename;
//triggering the function
downloadLink.click();
}
}
但是大部分在导出到excel的时候会被截掉:
【问题讨论】:
标签: javascript html regex excel html-table