(function ($) { //document.writeln(""); //document.writeln(""); $.fn.exportToExcel = function (option) { //if ($("script[src*='excellentexport.js']").length == 0) { // var scriptEle = document.createElement("script"); // scriptEle.src = "/Scripts/excellentexport.js"; // scriptEle.type = 'text/javascript'; // document.getElementsByTagName('head')[0].appendChild(scriptEle); //} var callbackFunc = function () { var defaultOp = { url: null, postData: {}, colModel: [], remote: false, data: [], sheetName: "sheet1", fileName: "file", fileType: "xls", groupHeader: null, numberWithTab: true }; var op = $.extend(defaultOp, option); var getValByFieldName = function (model, fieldName) { if (model == undefined || model == null || typeof model != "object") { return model; } if (model.hasOwnProperty(fieldName)) { return model[fieldName]; } var index = fieldName.indexOf("."); var frontFieldName = fieldName.substr(0, index); var backFieldName = fieldName.substr(index + 1); return getValByFieldName(model[frontFieldName], backFieldName); } var exportDataToExcel = function (data) { var colModel = op.colModel; var groupHeader = op.groupHeader; var groupHeaders, groupIndex, isInGroup, groupColumnCount; if (groupHeader) { groupHeaders = groupHeader.groupHeaders; groupIndex = 0; isInGroup = false; groupColumnCount = 0; } if (colModel.length <= 0) { return; } var html = ""; html += ""; if (groupHeader) { for (var i = 0; i < colModel.length; i++) { var colSpan = ""; if (groupHeaders[groupIndex] && groupHeaders[groupIndex].startColumnName == colModel[i].fieldName) { isInGroup = true; colSpan = `colspan='${groupHeaders[groupIndex].numberOfColumns}'`; html += "".template(colSpan, groupHeaders[groupIndex].title); groupColumnCount++; } else if (groupHeaders[groupIndex] && isInGroup) { groupColumnCount++; if (groupColumnCount >= groupHeaders[groupIndex].numberOfColumns) { groupIndex++; isInGroup = false; groupColumnCount = 0; } } else { html += "".template(colModel[i].colName); } } html += ""; isInGroup = false; groupIndex = 0; groupColumnCount = 0; for (var i = 0; i < colModel.length; i++) { if (groupHeaders[groupIndex] && groupHeaders[groupIndex].startColumnName == colModel[i].fieldName) { html += "".template(colModel[i].colName); isInGroup = true; groupColumnCount++; } else if (groupHeaders[groupIndex] && isInGroup) { html += "".template(colModel[i].colName); groupColumnCount++; if (groupColumnCount >= groupHeaders[groupIndex].numberOfColumns) { groupIndex++; isInGroup = false; groupColumnCount = 0; } } } } else { for (var i = 0; i < colModel.length; i++) { html += "".template(colModel[i].colName); } } html += ""; html += ""; if (data.length > 0) { for (var i = 0; i < data.length; i++) { html += ""; for (var j = 0; j < colModel.length; j++) { var fieldName = colModel[j].fieldName; var colValue = getValByFieldName(data[i], fieldName); var styleStr = ""; if (colModel[j].cellattr && typeof colModel[j].cellattr == "function") { styleStr = colModel[j].cellattr(colValue, data[i], j); } if (colValue !== "" && colModel[j].formatter) { if (typeof colModel[j].formatter == "string") { switch (colModel[j].formatter) { case "number": colValue = numeral(colValue).format("0.00"); if (colValue === "NaN") { colValue = "0.00"; } else if (op.fileType == "csv" && op.numberWithTab) { colValue = colValue; //先把前面的\t去掉,有需求的时候再改 } break; case "number2": colValue = numeral(colValue).format("0,0.00"); if (colValue === "NaN") { colValue = "0.00"; } break; case "date": if (colValue == undefined || colValue == null) { colValue = ""; } else { colValue = moment(colValue).format("YYYY-MM-DD"); } break; case "datetime": if (colValue == undefined || colValue == null) { colValue = ""; } else { colValue = moment(colValue).format("YYYY-MM-DD HH:mm:ss"); } break; case "text": if (colValue == undefined || colValue == null) { colValue = ""; } else { colValue = colValue; } if (!styleStr) { styleStr = "style="; } styleStr += "\"mso-number-format:'\@';\""; break; } } else if (typeof colModel[j].formatter == "function") { colValue = colModel[j].formatter(colValue, data[i], j); } } if (colValue == undefined || colValue == null) { colValue = ""; } html += "".template(colValue); } html += ""; } } html += "
{1}{0}
{0}{0}{0}
{0}
"; if ($("#createInvote99").length <= 0) { $(document.body) .append( '' .template(op.fileName + "." + op.fileType)); $('#createInvote99').click( function () { var tempTrim = String.prototype.trim; String.prototype.trim = function () { var firstChar = ""; if (this.indexOf("\t") == 0) { firstChar = "\t"; } return firstChar + this.replace(/(^\s*)|(\s*$)/g, ""); }; if (op.fileType == "xls") { ExcellentExport.excel(this, 'exportTable99', op.sheetName); } else { ExcellentExport.csv(this, 'exportTable99'); } String.prototype.trim = tempTrim; } ); } $("#createInvote99").attr("download", op.fileName + "." + op.fileType); if ($("#exportDiv99").length <= 0) { $(document.body).append("".template(html)); } else { $("#exportDiv99").html(html); } document.getElementById('createInvote99').click(); } if (op.remote) { main.post(op.url, op.postData).done(function (result) { if (result.success && result.obj) { exportDataToExcel(result.obj); } else if (result instanceof Array) { exportDataToExcel(result); } else if (!main.isEmpty(result.rows)) { exportDataToExcel(result.rows); } }); } else if (!main.isEmpty(op.data)) { exportDataToExcel(op.data); } } if ($("script[src*='excellentexport.js']").length == 0) { main.loadJs("/Statics/libs/export/excellentexport.js", callbackFunc); } else { callbackFunc(); } } })(jQuery)