解决fastadmin自带导出excel打开报文件格式和扩展名不匹配的问题

      发布在:前端技术      评论:0 条评论

5dca2ecc-6199-4e09-bd6f-4b3c1a3cb7d6

虽然可以打开看到里面的内容,但是给人的感觉就是很不爽,于是分析下代码找到了解决方法

找到文件public/assets/libs/bootstrap-table/dist/extensions/export/bootstrap-table-export.js

大概在97行

把代码

require(['tableexport'], function () {
that.$el.tableExport($.extend({}, that.options.exportOptions, {
type: type,
       escape: false
   }));
});

替换成

if(type == 'excel'){
require(['tableexport','/assets/libs/tableExport.jquery.plugin/libs/js-xlsx/xlsx.core.min.js'], function () {
that.$el.tableExport($.extend({}, that.options.exportOptions, {
type:'excel',
           mso: { fileFormat: 'xlsx' }
}));
   });
}else{
require(['tableexport'], function () {
that.$el.tableExport($.extend({}, that.options.exportOptions, {
type: type,
           escape: false
       }));
   });
}

这里需要引入文件

/assets/libs/tableExport.jquery.plugin/libs/js-xlsx/xlsx.core.min.js

所以可以去https://github.com/hhurz/tableExport.jquery.plugin下载引入

后端引入的是压缩包文件,所以这里需要把文件public/assets/libs/bootstrap-table/dist/extensions/export/bootstrap-table-export.js压缩成public/assets/libs/bootstrap-table/dist/extensions/export/bootstrap-table-export.min.js文件

最后需要在网站根路径命令窗口下执行php think min -m all -r all

针对fastadmin版本1.6.1.20250430 的写法如下

require(['tableexport','/assets/libs/tableExport.jquery.plugin/libs/js-xlsx/xlsx.core.min.js'], function () {
var type = $(li).data('type'),doExport ;
console.log(type);
if(type == 'excel'){
doExport = function () {
that.$el.tableExport($.extend({}, that.options.exportOptions, {
type:'excel',
mso: { fileFormat: 'xlsx' }
}));
};
}else{
doExport = function () {
that.$el.tableExport($.extend({}, that.options.exportOptions, {
type: type,
escape: false
}));
};
}
console.log(doExport);

if (that.options.exportDataType === 'all' && that.options.pagination) {
that.$el.one(that.options.sidePagination === 'server' ? 'post-body.bs.table' : 'page-change.bs.table', function () {
doExport();
that.togglePagination();
});
that.togglePagination();
} else if (that.options.exportDataType === 'selected') {
var data = that.getData(),
selectedData = that.getAllSelections();

// Quick fix #2220
if (that.options.sidePagination === 'server') {
data = {total: that.options.totalRows};
data[that.options.dataField] = that.getData();
var Table = typeof require === 'function' ? require('table') : null;
selectedData = {total: that.options.totalRows};
selectedData[that.options.dataField] = Table && that.options.maintainSelected ? Table.api.selecteddata(that.$el) : that.getAllSelections();
}

that.load(selectedData);
doExport();
that.load(data);
} else {
doExport();
}
});
热门推荐