MWF.xApplication.Template = MWF.xApplication.Template || {}; MWF.xApplication.Template.utils = MWF.xApplication.Template.utils || {}; MWF.xApplication.Template.utils.ExcelUtils = new Class({ Implements: [Options, Events], options:{ 'isTemplate': false, 'headText': '', 'headStyle': { font: { name: '宋体', family: 4, size: 20, bold: true }, alignment: { vertical: 'middle', horizontal: 'center', wrapText: true } }, 'columnTitleStyle': { font: { name: '宋体', family: 4, size: 12, bold: true }, alignment: { vertical: 'middle', horizontal: 'center', wrapText: true } }, 'columnContentStyle': { font: { name: '宋体', family: 4, size: 12, bold: false }, alignment: { vertical: 'middle', horizontal: 'center', wrapText: true } } }, initialize: function( options ){ if(options)this.setOptions(options); this.sheet2JsonOptions = {}; this.pollyfill(); }, pollyfill: function(){ if (!FileReader.prototype.readAsBinaryString) { FileReader.prototype.readAsBinaryString = function (fileData) { var binary = ""; var pt = this; var reader = new FileReader(); reader.onload = function (e) { var bytes = new Uint8Array(reader.result); var length = bytes.byteLength; for (var i = 0; i < length; i++) { binary += String.fromCharCode(bytes[i]); } //pt.result - readonly so assign binary pt.content = binary; pt.onload(); }; reader.readAsArrayBuffer(fileData); } } }, _openDownloadDialog: function(url, saveName, callback){ /** * 通用的打开下载对话框方法,没有测试过具体兼容性 * @param url 下载地址,也可以是一个blob对象,必选 * @param saveName 保存文件名,可选 */ if( Browser.name !== 'ie' ){ if(typeof url == 'object' && url instanceof Blob){ url = URL.createObjectURL(url); // 创建blob地址 } var aLink = document.createElement('a'); aLink.href = url; aLink.download = saveName || ''; // HTML5新增的属性,指定保存文件名,可以不要后缀,注意,file:///模式下不会生效 var event; if(window.MouseEvent && typeOf( window.MouseEvent ) == "function" ) event = new MouseEvent('click'); else { event = document.createEvent('MouseEvents'); event.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null); } aLink.dispatchEvent(event); if(callback)callback(); }else{ window.navigator.msSaveBlob( url, saveName); if(callback)callback(); } }, index2ColName : function( index ){ if (index < 0) { return null; } var num = 65;// A的Unicode码 var colName = ""; do { if (colName.length > 0)index--; var remainder = index % 26; colName = String.fromCharCode(remainder + num) + colName; index = (index - remainder) / 26; } while (index > 0); return colName; }, upload : function ( dateColIndexArray, callback ) { var dateColArray = []; dateColIndexArray.each( function (idx) { dateColArray.push( this.index2ColName( idx )); }.bind(this)) var uploadFileAreaNode = new Element("div"); var html = ""; uploadFileAreaNode.set("html", html); var fileUploadNode = uploadFileAreaNode.getFirst(); fileUploadNode.addEvent("change", function () { var files = fileNode.files; if (files.length) { var file = files.item(0); // if( file.name.indexOf(" ") > -1 ){ // this.form.notice( MWF.xApplication.process.Xform.LP.uploadedFilesCannotHaveSpaces, "error"); // return false; // } //第三个参数是日期的列 this.importFromExcel( file, function(json){ //json为导入的结果 if(callback)callback(json); uploadFileAreaNode.destroy(); }.bind(this), dateColArray ); //["E","F"] } }.bind(this)); var fileNode = uploadFileAreaNode.getFirst(); fileNode.click(); }, _loadExportResource : function( callback ){ if( !window.ExcelJS ){ var uri = "../o2_lib/exceljs/babel-polyfill-6.2.js"; var uri2 = "../o2_lib/exceljs/exceljs.min.js"; COMMON.AjaxModule.load(uri, function(){ COMMON.AjaxModule.load(uri2, function(){ callback(); }.bind(this)) }.bind(this)) }else{ callback(); } }, exportToExcel : function(array_arg, fileName_arg, colWidthArr_arg, dateIndexArray_arg, numberIndexArray_arg, callback){ // var array = [["姓名","性别","学历","专业","出生日期","毕业日期"]]; // array.push([ "张三","男","大学本科","计算机","2001-1-2","2019-9-2" ]); // array.push([ "李四","男","大学专科","数学","1998-1-2","2018-9-2" ]); // this.exportToExcel(array, "导出数据"+(new Date).format("db")); if( !MWF.xApplication.Template.LP ){ MWF.xDesktop.requireApp("Template", "lp." + MWF.language, null, false); } this._loadExportResource(function (){ var workbook = new ExcelJS.Workbook(); var sheet = workbook.addWorksheet('Sheet1'); //sheet.properties.defaultRowHeight = 25; var arg = { offsetColumnIndex: 0, offsetRowIndex: 0, sheet: sheet, fileName: fileName_arg, array: array_arg, colWidthArr: colWidthArr_arg, dateIndexArray: dateIndexArray_arg, numberIndexArray: numberIndexArray_arg, excelUtilsObject: this }; this.fireEvent('beforeAppendData', [arg]); var offsetColumnIndex = arg.offsetColumnIndex; var offsetRowIndex = arg.offsetRowIndex; var array = arg.array; var colWidthArr = arg.colWidthArr; var dateIndexArray = arg.dateIndexArray; var numberIndexArray = arg.numberIndexArray; var titleArray = array[0]; this.appendDataToSheet(sheet, array, colWidthArr, dateIndexArray, numberIndexArray, offsetColumnIndex, offsetRowIndex); var hasValidation = false; var ps = titleArray.map(function( title ){ if( o2.typeOf(title) === 'object' && title.options ){ hasValidation = true; return title.options; } return null; }); if(hasValidation){ Promise.all(ps).then(function(args){ for( var i=0; i