zoukankan      html  css  js  c++  java
  • 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)));
  • 相关阅读:
    Extjs4单选按钮
    下拉框comboxBox本地数据源示例
    处理您的请求时发生异常。此外,对第一个异常执行自定义错误页时发生另一异常。已终止请
    hoj1568
    hoj3434
    poj2924
    poj2909
    hoj3433
    理解Python闭包,这应该是最好的例子
    Spring 学习笔记
  • 原文地址:https://www.cnblogs.com/zhengqian/p/14103019.html
Copyright © 2011-2022 走看看