fix: 修复Excel导出样式异常问题

This commit is contained in:
邹景立 2025-03-11 23:26:32 +08:00
parent 1617be2961
commit 8c374c7424

View File

@ -20,13 +20,13 @@ define(function () {
} }
/*! 默认导出配置 */ /*! 默认导出配置 */
Excel.prototype.options = {writeOpt: {bookSST: true}}; Excel.prototype.options = {writeOpt: {bookSST: false}};
/*! 导出 Excel 文件 */ /*! 导出 Excel 文件 */
Excel.prototype.export = function (data, name, options) { Excel.prototype.export = function (data, name, options) {
if (data === false || data === null) return this; name = name || '数据导出_' + layui.util.toDateString(Date.now(), '_yyyyMMdd_HHmmss');
if (name.substring(0, -5).toLowerCase() !== '.xlsx') name += '.xlsx'; if (name.substring(0, -5).toLowerCase() !== '.xlsx') name += '.xlsx';
layui.excel.exportExcel(data, name, 'xlsx', options || this.options || {writeOpt: {bookSST: true}}); layui.excel.exportExcel(data, name, 'xlsx', $.extend(options || {}, this.options));
}; };
/*! 绑定导出的事件 */ /*! 绑定导出的事件 */
@ -41,30 +41,24 @@ define(function () {
// </script> // </script>
Excel.prototype.bind = function (done, filename, selector, options) { Excel.prototype.bind = function (done, filename, selector, options) {
let that = this; let that = this;
this.options = options || {} this.options = $.extend(this.options, options || {});
this.bindLoadDone(function (data) { this.bindLoadDone(function (data, button) {
that.export(data, this.dataset.filename || filename, options); that.export(done.call(that, data, []), button.dataset.filename || filename);
}, selector); }, selector);
}; };
/** /*! 加载所有数据 */
* 加载所有数据
* @param done
* @param selector
*/
Excel.prototype.bindLoadDone = function (done, selector) { Excel.prototype.bindLoadDone = function (done, selector) {
let that = this; let that = this;
$('body').off('click', selector || '[data-form-export]').on('click', selector || '[data-form-export]', function () { $('body').off('click', selector || '[data-form-export]').on('click', selector || '[data-form-export]', function () {
let button = this, form = $(button).parents('form'); let button = this, form = $(this).parents('form');
let method = button.dataset.method || form.attr('method') || 'get'; let method = this.dataset.method || form.attr('method') || 'get';
let location = button.dataset.excel || button.dataset.formExport || form.attr('action') || ''; let location = this.dataset.excel || this.dataset.formExport || form.attr('action') || '';
let sortType = $(button).attr('data-sort-type') || '', sortField = $(button).attr('data-sort-field') || ''; let sortType = $(this).attr('data-sort-type') || '', sortField = $(this).attr('data-sort-field') || '';
if (sortField.length > 0 && sortType.length > 0) { if (sortField.length > 0 && sortType.length > 0) {
location += (location.indexOf('?') > -1 ? '&' : '?') + '_order_=' + sortType + '&_field_=' + sortField; location += (location.indexOf('?') > -1 ? '&' : '?') + '_order_=' + sortType + '&_field_=' + sortField;
} }
that.load(location, form.serialize(), method).then(function (data) { that.load(location, form.serialize(), method).then((data) => done.call(that, data, button)).fail(function (ret) {
done.call(button, data, []);
}).fail(function (ret) {
$.msg.tips(ret || '数据加载失败'); $.msg.tips(ret || '数据加载失败');
}); });
}); });