html+js实现导出多sheet的excel

let classList = [{className: "exBody1", title: "产品单", type: 1}, {
    className: "exBody2", title: "下料单", type: 2
}, {className: "exBody3", title: "开料单", type: 3}, {
    className: "exBody4", title: "掩门单", type: 4
}, {className: "exBody5", title: "移门单", type: 5}, {
    className: "exBody6", title: "五金单", type: 6
}, {className: "exBody7", title: "线条单", type: 7}];
let Worksheet = '';
let htmlList = '';
let HRefList = '';
for (const cKey in classList) {
    let info = classList[cKey];
    // let tableHtml = document.getElementsByClassName(info.className);
    let tableHtml = document.getElementById(info.className);
    // 使用outerHTML属性获取整个table元素的HTML代码(包括标签),然后包装成一个完整的HTML文档,设置charset为urf-8以防止中文乱码
    let appendHtml = tableHtml.innerHTML;
    if (appendHtml.length < 600) {
        continue;
    }
    Worksheet += '' + info.title + '';
    htmlList += toZhExcelTable(info.className, appendHtml);
    HRefList += '';
}
let txt = 'MIME-Version: 1.0\n' + 'X-Document-Type: Workbook\n' + 'Content-Type: multipart/related; boundary="----=_NextPart_dummy"\n' + '\n' + '------=_NextPart_dummy\n' + 'Content-Location: WorkBook.htm\n' + 'Content-Type: text/html; charset=utf-8\n' + '\n' + '\n' + '\n' + '\n' + '\n' + '\n' + '\n' + '\n' + '\n' + '    \n' + '    <body><p>This page uses frames, but your browser does not support them.</p></body>\n' + '\n' + ''
txt += htmlList;
txt += 'Content-Location: filelist.xml\n' + 'Content-Type: text/xml; charset="utf-8"\n' + '\n' + '\n' + '    \n' + HRefList + '\n' + '\n' + '------=_NextPart_dummy--';

// // 实例化一个Blob对象,其构造函数的第一个参数是包含文件内容的数组,第二个参数是包含文件类型属性的对象
let blob = new Blob([txt], {
    type: "text/plain;charset=utf-8",
}); //application/octet-stream
//也可以用js创建一个a标签
let a = document.createElement('a');
// 利用URL.createObjectURL()方法为a元素生成blob URL
a.href = URL.createObjectURL(blob);
// 设置文件名
a.download = title + ".xls"; //xlsx
a.click(); 

                            
                        
                    
                    
                    

你可能感兴趣的:(html+js实现导出多sheet的excel)