js导出数据为excel表格

function SE_ExportScheme() {

    SavePic();

    var rows = $("#objlist").datagrid("getRows");
    if (rows.length < 2) {
        spanMsg.innerHTML = "至少需要设置两个相位";
        return;
    }

    // 检测绿冲突
    var result = DetectGreenConflict(rows);
    if (result.num != -1) {
        spanMsg.innerHTML = result.msg;
        $("#objlist").datagrid("selectRow", result.num);
        var row = $('#objlist').datagrid('getSelected');
        var index = $('#objlist').datagrid('getRowIndex', row);
        editObjlist(index, row);
        LastRowIndex = index;

        return;
    }

    // var postPbjects = '[';
    // for (var i = 0; i < rows.length; i = i + 1) {
    //     if (i > 0) { postPbjects += ','; }
    //     postPbjects += '{"phaseno": "' + CheckUI(rows[i].phaseno) + '", "stageid": "' + CheckUI(rows[i].stageid) + '","nlight": "' + rows[i].nlight + '", "elight":"' + rows[i].elight + '", "slight":"' + rows[i].slight
    //         + '" ,"wlight": "' + rows[i].wlight + '", "greentime": "' + CheckUI(rows[i].greentime) + '","greenflashtime": "' + CheckUI(rows[i].greenflashtime) + '", "mingreen":"' + CheckUI(rows[i].mingreen) + '", "maxgreen":"' + CheckUI(rows[i].maxgreen)
    //         + '" ,"senseinterval": "' + CheckUI(rows[i].senseinterval) + '", "phaseAdjustStep": "' + CheckUI(rows[i].phaseAdjustStep) + '","maxcount": "' + CheckUI(rows[i].maxcount) + '", "isMain":"' + CheckUI(rows[i].isMain) + '", "yellowtime":"' + CheckUI(rows[i].yellowtime)
    //         + '" ,"yellowflashtime": "' + CheckUI(rows[i].yellowflashtime) + '", "redyellowHintTime": "' + CheckUI(rows[i].redyellowHintTime) + '","allredtime": "' + CheckUI(rows[i].allredtime) + '", "pedgreen":"' + CheckUI(rows[i].pedgreen) + '", "pedgreenflash":"' + CheckUI(rows[i].pedgreenflash)
    //         + '" ,"pedearlyend":"' + CheckUI(rows[i].pedearlyend) + '" ,"peddalay":"' + CheckUI(rows[i].peddalay) + '" ,"personbutton":"' + CheckUI(rows[i].personbutton) + '" ,"iscoor":"' + CheckUI(rows[i].iscoor) + '", "coortime":"' + CheckUI(rows[i].coortime) + '"}';
    // }
    // postPbjects += ']';

    // var SIGID = $("#sigid").val();
    // var FileName = null;
    // for (var i = 0; i < utcs.length; i++) {
    //     if (utcs[i].sigid == SIGID) {
    //         FileName = utcs[i].name + '-配时方案-' + $("#nowGscheme").html() + '.txt';
    //     }
    // }
    var jsonData=[];

    for (var i = 0; i < rows.length; i = i + 1) {
    
     // 要导出的json数据
     var model =
        {
            name:CheckUI(rows[i].phaseno),
            phone:CheckUI(rows[i].stageid),
            email: rows[i].nlight 
        }
        jsonData.push(model);
}
    // 列标题var
    let str = '<tr><td>姓名</td><td>电话</td><td>邮箱</td></tr>';
    // 循环遍历,每行加入tr标签,每个单元格加td标签
    for(let i = 0 ; i < jsonData.length ; i++ ){
        str+='<tr>';
        for(const key in jsonData[i]){
            // 增加	为了不让表格显示科学计数法或者其他格式
            str+=`<td>${ jsonData[i][key] + '	'}</td>`;     
        }
        str+='</tr>';
    }
    // Worksheet名
    const worksheet = 'Sheet1'
    const uri = 'data:application/vnd.ms-excel;base64,';

    // 下载的表格模板数据
    const 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>${str}</table></body></html>`;
    // 下载模板
    window.location.href = uri + base64(template);

}


 
    // 输出base64编码
    const base64 = s => window.btoa(unescape(encodeURIComponent(s)));
原文地址:https://www.cnblogs.com/zhengqian/p/14103019.html