• 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)));
  • 相关阅读:
    nginx技术总结
    剑指offer学习摘录面试2 单例模式 JoannaIn
    visual stadio 编译c++文件 JoannaIn
    Git生成SSH密钥
    spring——自动装配【非常详细】
    @Target:注解的作用目标
    SSH客户端神器之 MobaXterm
    Maven 最全教程,看了必懂,99% 的人都收藏了!
    必须了解的MySQL三大日志:binlog、redo log和undo log
    MyBatis批量插入几千条数据,请慎用foreach
  • 原文地址:https://www.cnblogs.com/zhengqian/p/14103019.html
Copyright © 2020-2023  润新知