js 导出表格

导出为excel

  • 将原始数据渲染为 table 标签
  • 导出
1
2
3
4
5
6
7
8
var html = document.querySelecor('#export-table').outerHTML;
// 实例化一个 Blob 对象,其构造函数的第一个参数是包含文件内容的数组,第二个参数是包含文件类型属性的对象
var blob = new Blob([html], { type: 'application/vnd.ms-excel' });
var a = document.querySelecor('#export-btn');
// 利用 URL.createObjectURL() 方法为a元素生成 blob URL
a.href = URL.createObjectURL(blob);
// 设置文件名,目前只有 Chrome 和 FireFox 支持此属性
a.download = "导出.xls";
0%