var Excel =
{
open : function(data, filename, sheetname) {
this.name = sheetname || 'sheet1';
this.sheet = XLSX.utils.aoa_to_sheet(data, {raw: true});
this.autoWidthFunc(this.sheet, data);
if (filename.substr(-5).toLowerCase() !== '.xlsx') {
filename += '.xlsx';
}
this.openDownloadDialog(this.sheet2blob(this.sheet, this.name), filename);
},
autoWidthFunc : function (ws, data) {
// set worksheet max width per col
const colWidth = data.map(row =>
row.map(val => {
// if null/undefined
if (val == null) {
return { wch: 10 };
} else if (val.toString().charCodeAt(0) > 255) {
// if chinese
var l = val.toString().length;
var n = l + l*0.5;
return { wch: n * 2 };
} else {
var l = val.toString().length;
var n = l + l*0.5;
return { wch: n };
}
})
);
// start in the first row
const result = colWidth[0];
for (let i = 1; i < colWidth.length; i++) {
for (let j = 0; j < colWidth[i].length; j++) {
if (result[j].wch < colWidth[i][j].wch) {
result[j].wch = colWidth[i][j].wch;
}
}
}
ws['!cols'] = result;
},
export : function (url, data) {
var self = this;
var filename = '';
var head = [];
var alldata = [];
var loading = layer.msg('正在加载 ,完成0%', {time:0,icon: 1});
nextPage(1, 1);
function nextPage(curPage, maxPage) {
if (curPage > maxPage) {
alldata.unshift(head);
this.result = alldata;
if (this.result !== false) {
self.open(this.result, filename || '文件下载.xlsx');
} else {
console.log('格式化函数返回`false`,已终止数据导出操作', alldata, this.result);
}
layer.close(loading);
} else {
$.post(url, {excel_type:1,pg:curPage,json:1}, function (ret) {
var ret = eval('(' + ret + ')');
if (ret.status == 1 && ret.data) {
filename = ret.data.filename;
head = ret.data.head;
alldata = alldata.concat(ret.data.body);
curPage = ret.page.current_page;
maxPage = ret.page.total_page;
$('[data-upload-page]').html(curPage + '/' + maxPage);
$('[data-upload-progress]').html((curPage / maxPage * 100).toFixed(2));
return nextPage(curPage + 1, maxPage, false);
} else {
layer.close(loading);
layer.alert(ret.msg);
}
}, false);
}
}
},
/*! Sheet 转下载对象 */
sheet2blob : function(sheet, name) {
this.workbook = {SheetNames: [name], Sheets: {}};
this.workbook.Sheets[name] = sheet;
this.content = XLSX.write(this.workbook, {
type: 'binary', bookSST: false, bookType: 'xlsx',
});
return new Blob([this.toArrayBuffer(this.content)], {
type: "application/octet-stream"
});
},
/*! 字符串转 ArrayBuffer */
toArrayBuffer : function(s) {
this.buff = new ArrayBuffer(s.length);
this.view = new Uint8Array(this.buff);
for (this.index = 0; this.index !== s.length; ++this.index) {
this.view[this.index] = s.charCodeAt(this.index) & 0xFF;
}
return this.buff;
},
/*! 通用的打开下载对话框方法 */
openDownloadDialog : function(location, filename) {
if (typeof location == 'object' && location instanceof Blob) {
location = URL.createObjectURL(location);
}
this.link = document.createElement('a');
this.link.download = filename || Date.now() + '.xlsx';
this.link.href = location;
if (window.MouseEvent) {
this.event = new MouseEvent('click');
} else {
this.event = document.createEvent('MouseEvents');
this.event.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
}
this.link.dispatchEvent(this.event);
}
}