Files
zszq-trs/YLErpWeb/wwwroot/Statics/libs/v1/exportUtil.js
T
2024-05-09 14:06:26 +08:00

226 lines
11 KiB
JavaScript

(function ($) {
//document.writeln("<script src='/Scripts/excellentexport.js' type='text/javascript'></script>");
//document.writeln("<script>alert(1)</script>");
$.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 = "<table id='exportTable99'>";
html += "<thead><tr>";
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 += "<th {0}>{1}</th>".template(colSpan, groupHeaders[groupIndex].title);
groupColumnCount++;
} else if (groupHeaders[groupIndex] && isInGroup) {
groupColumnCount++;
if (groupColumnCount >= groupHeaders[groupIndex].numberOfColumns) {
groupIndex++;
isInGroup = false;
groupColumnCount = 0;
}
} else {
html += "<th rowspan='2'>{0}</th>".template(colModel[i].colName);
}
}
html += "</tr><tr>";
isInGroup = false;
groupIndex = 0;
groupColumnCount = 0;
for (var i = 0; i < colModel.length; i++) {
if (groupHeaders[groupIndex] && groupHeaders[groupIndex].startColumnName == colModel[i].fieldName) {
html += "<th>{0}</th>".template(colModel[i].colName);
isInGroup = true;
groupColumnCount++;
} else if (groupHeaders[groupIndex] && isInGroup) {
html += "<th>{0}</th>".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 += "<th>{0}</th>".template(colModel[i].colName);
}
}
html += "</tr></thead>";
html += "<tbody>";
if (data.length > 0) {
for (var i = 0; i < data.length; i++) {
html += "<tr>";
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 += "<td " + styleStr + ">{0}</td>".template(colValue);
}
html += "</tr>";
}
}
html += "</tbody></table>";
if ($("#createInvote99").length <= 0) {
$(document.body)
.append(
'<a onfocus="this.blur(); " style="display: none; " download="{0}" id="createInvote99" class="ipt - todo hide">code</a>'
.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("<div style='display:none' id='exportDiv99'>{0}</div>".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)