欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

把表格数据导出到excel中

程序员文章站 2022-03-15 12:35:06
...
//用于封装导出数据源
jQuery.ExcelData = function (E_data, ExcelFileName) {   
    var ret = ".";
    try {
        //读取E_Header
        var HeaderJson = "";
        var E_html = "";
        $(".easyui_list_container table:eq(1) tbody tr td").each(function () {         
            if ($(this).css("display") != "none") {
                var datafield = $(this).attr("field");
                var headname = $.trim($(this).children().eq(0).children().eq(0).text());
              
                var showType = "text";
                HeaderJson += "{\"headname\":\"" + headname + "\",\"datafield\":\"" + datafield + "\",\"showtype\":\"" + showType + "\"},";
            }                   
        });
      
        if (HeaderJson != "" && E_data.rows != null && E_data.rows.length > 0) {
          
            HeaderJson = "[" + HeaderJson.substring(0, HeaderJson.length - 1) + "]";
            var E_Header = JSON.parse(HeaderJson);
            $.each(E_data.rows, function (E_Index, E_item) {
                var E_Itemhtml = "{";
                $.each(E_Header, function (headIndex, headItem) {
                    if (headItem.showtype == "time") {
                        if (E_item[headItem.datafield] == undefined || E_item[headItem.datafield] == null || E_item[headItem.datafield].indexOf("Date(-") >= 0) {
                            E_Itemhtml += "\"" + headItem.datafield + "\":\"\",";
                        }
                        else {
                            var fieldTimeObj = E_item[headItem.datafield];
                            if (fieldTimeObj.indexOf("Date") >= 0) {
                                fieldTimeObj = eval('new ' + fieldTimeObj.substr(1, fieldTimeObj.length - 2)).Format("yyyy-MM-dd");
                                //fieldTimeObj.replace(/Date\([\d+]+\)/, function (a) { eval('d = new ' + a) });                                  
                                E_Itemhtml += "\"" + headItem.datafield + "\":\"" + fieldTimeObj + "\",";
                            }
                            else {
                                E_Itemhtml += "\"" + headItem.datafield + "\":\"" + new Date(fieldTimeObj.replace(/-/g, "/")).fieldFormat("yyyy-MM-dd hh:mm:ss") + "\",";
                            }
                        }

                    }
                    //else if (headItem.showtype == "bool") {
                    //    E_Itemhtml += "\"" + headItem.datafield + "\":\"" + (E_item[headItem.datafield] == true ? "True" : "False") + "\",";
                    //}
                    else {
                        if (E_item[headItem.datafield] == undefined || E_item[headItem.datafield] == null) {
                            E_Itemhtml += "\"" + headItem.datafield + "\":\"\",";
                        }
                        else {
                            E_Itemhtml += "\"" + headItem.datafield + "\":\"" + E_item[headItem.datafield] + "\",";
                        }
                    }

                });
                E_Itemhtml = E_Itemhtml.substring(0, E_Itemhtml.length - 1) + "},";
                E_html += E_Itemhtml;
            });

        }
        if (HeaderJson != "" && E_html != undefined && E_html != null && E_html != "") {
            E_html = "[" + E_html.substring(0, E_html.length - 1) + "]";          
            //导出到Excel表中
            $.post("/Common/JsonToExcel", { JsonHeader: encodeURI(HeaderJson), JsonData: escape(E_html), filename: ExcelFileName + ".xls" }, function (filedata) {
                if (filedata != "") {
                    window.location.href = filedata;
                }
            });
        }
        else {
            $.messager.alert("提示", "无数据导出!", "error");
        }
    }
    catch (e) {

    }
    return ret;
}  


//时间格式转换
Date.prototype.Format = function (fmt) { //author: meizz
    var o = {
        "M+": this.getMonth() + 1, //月份
        "d+": this.getDate(), //日
        "h+": this.getHours(), //小时
        "m+": this.getMinutes(), //分
        "s+": this.getSeconds(), //秒
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度
        "S": this.getMilliseconds() //毫秒
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}
<a href="#" class="easyui-linkbutton" onclick="Export()">导出</a>



  //导出
    function Export(){
        $.ajax({
            url: '@Url.Action("GetPowerOffInfo", "PowerOff")',
            data: {
                fromDate: $("#s1").val(),
                toDate: $("#e1").val(),
                collieryName: $("#OutageQueryMineName").text()
            },
            type: "post",
            success: function (m_data) {
                if (m_data.Data != null) {
                    $.ExcelData(m_data.Data, $("#OutageQueryMineName").text() +"断电查询(" + $("#s1").val() + '至' + $("#e1").val()+")");
                }
            },
            error: function (a) {
            	 if (a) console.log(a); 
             }
        })
    }
相关标签: 导出