我对JS没那么好,也不熟悉。
我在网上读到关于将表中的数据导出到excel 从以下文章中的内容。
在那里,他们使用了这个函数示例。
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();
}
}在这里,我无法理解以下几行
var tableHTML = tableSelect.outerHTML.replace(/ /g, '%20');var blob = new Blob(['\ufeff', tableHTML], {type: dataType }); navigator.msSaveOrOpenBlob( blob, filename);有人能帮我理解它吗。
发布于 2019-03-22 17:24:32
第一个元素执行基本的URL编码,用它们的URL等效%20替换空格。
第二件事是创建Blob实例,它以与十六进制feff对应的unicode字符开始,然后跟着tableHTML变量的内容。
最后,navigator调用导致浏览器提示用户下载文件。
编辑:
请您详细分享(/ /g,'%20')
正如@Felix所指出的,这是一个JavaScript正则表达式。JavaScript中的内联正则表达式以正斜杠开头,以正斜杠结尾。g意味着它是全局应用的,而不是第一次匹配。
https://stackoverflow.com/questions/55304725
复制相似问题