mirror of
https://gitee.com/zoujingli/ThinkAdmin.git
synced 2025-04-29 21:06:35 +08:00
Update excel.xlsx.js
This commit is contained in:
parent
a6f3eafce8
commit
7ba20584c5
@ -2,32 +2,30 @@ define(['xlsx'], function () {
|
|||||||
|
|
||||||
function excel(data, filename, sheetname) {
|
function excel(data, filename, sheetname) {
|
||||||
this.name = sheetname || 'sheet1';
|
this.name = sheetname || 'sheet1';
|
||||||
this.sheet = XLSX.utils.aoa_to_sheet(data);
|
this.work = {SheetNames: [this.name], Sheets: {}};
|
||||||
if (filename.substr(-5).toLowerCase() !== '.xlsx') {
|
this.work.Sheets[this.name] = XLSX.utils.aoa_to_sheet(data);
|
||||||
filename += '.xlsx';
|
if (filename.substr(-5).toLowerCase() !== '.xlsx') filename += '.xlsx';
|
||||||
}
|
XLSX.writeFile(this.work, filename);
|
||||||
openDownloadDialog(sheet2blob(this.sheet, this.name), filename);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
excel.bind = function (done, filename) {
|
excel.bind = function (done, filename) {
|
||||||
$('body').off('click', '[data-form-export]').on('click', '[data-form-export]', function () {
|
$('body').off('click', '[data-form-export]').on('click', '[data-form-export]', function () {
|
||||||
var form = $(this).parents('form');
|
var form = $(this).parents('form');
|
||||||
var method = $(this).attr('data-method') || 'get';
|
var method = this.dataset.method || form.attr('method') || 'get';
|
||||||
var location = $(this).attr('data-form-export') || form.get(0).action || '';
|
var location = this.dataset.excel || this.dataset.formExport || form.attr('action') || '';
|
||||||
excel.load(location, form.serialize(), done, filename, method);
|
excel.load(location, form.serialize(), done, this.dataset.filename || filename, method);
|
||||||
})
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
excel.load = function (url, data, done, filename, method) {
|
excel.load = function (url, data, done, filename, method) {
|
||||||
var alldata = [];
|
var alldata = [];
|
||||||
var loading = $.msg.loading('正在加载 <span data-upload-page></span>,完成<span data-upload-progress>0</span>%');
|
var loading = $.msg.loading('正在加载 <span data-upload-page></span>,完成 <span data-upload-progress>0</span>%');
|
||||||
nextPage(1, 1);
|
nextPage(1, 1);
|
||||||
|
|
||||||
function nextPage(curPage, maxPage) {
|
function nextPage(curPage, maxPage) {
|
||||||
if (curPage > maxPage) {
|
if (curPage > maxPage) {
|
||||||
if (typeof done === 'function') {
|
if (typeof done === 'function') {
|
||||||
this.result = done(alldata);
|
if ((this.result = done(alldata)) !== false) {
|
||||||
if (this.result !== false) {
|
|
||||||
excel(this.result, filename || '文件下载.xlsx');
|
excel(this.result, filename || '文件下载.xlsx');
|
||||||
} else {
|
} else {
|
||||||
console.log('格式化函数返回`false`,已终止数据导出操作', alldata, this.result);
|
console.log('格式化函数返回`false`,已终止数据导出操作', alldata, this.result);
|
||||||
@ -37,7 +35,7 @@ define(['xlsx'], function () {
|
|||||||
}
|
}
|
||||||
$.msg.close(loading);
|
$.msg.close(loading);
|
||||||
} else {
|
} else {
|
||||||
$('[data-upload-page]').html(curPage + '/' + maxPage);
|
$('[data-upload-page]').html(curPage + ' / ' + maxPage);
|
||||||
$('[data-upload-progress]').html((curPage / maxPage * 100).toFixed(2));
|
$('[data-upload-progress]').html((curPage / maxPage * 100).toFixed(2));
|
||||||
$.form.load(url + (url.indexOf('?') > -1 ? '&' : '?') + 'output=json&page=' + curPage, data, method || 'get', function (ret) {
|
$.form.load(url + (url.indexOf('?') > -1 ? '&' : '?') + 'output=json&page=' + curPage, data, method || 'get', function (ret) {
|
||||||
if (ret.code) {
|
if (ret.code) {
|
||||||
@ -48,45 +46,5 @@ define(['xlsx'], function () {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return excel;
|
return excel;
|
||||||
|
|
||||||
/*! Sheet 转下载对象 */
|
|
||||||
function sheet2blob(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([toArrayBuffer(this.content)], {
|
|
||||||
type: "application/octet-stream"
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/*! 字符串转 ArrayBuffer */
|
|
||||||
function toArrayBuffer(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;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*! 通用的打开下载对话框方法 */
|
|
||||||
function openDownloadDialog(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);
|
|
||||||
}
|
|
||||||
});
|
});
|
Loading…
x
Reference in New Issue
Block a user