1、在vue中使用导入导出,需要下载3个依赖包和2个js包:
npm install -S file-saver xlsx(这里其实安装了2个依赖)
npm install -D script-loader
2、在src目录下新建一个excel文件夹引入Blob.js和expor2Excal.js
3、在main.js中全局引入
import Blob from ‘./excel/Blob.js’
import Export2Excel from ‘./excel/Export2Excel.js’
4、webpack.base.conf.js做如下修改:
resolve: {
extensions: ['.js', '.vue', '.json'],
alias: {
'vue$': 'vue/dist/vue.esm.js',
'@': resolve('src'),
'scss_vars': '@/styles/vars.scss',
//新增加一行
'excel': path.resolve(__dirname, '../src/excel'),
}
},
5、导出Excel
导出方法
//导出excel
//_tHeader :['名称','性别']
//_filterVal:['name','sex']
//_exData:需导出内容
//_fileName:文件名称
export const export2Excel = function (_tHeader, _filterVal, _exData, _fileName) {
require.ensure([], () => {
const {
export_json_to_excel } = require('../excel/Export2Excel'); //这里必须使用绝对路径
const tHeader = _tHeader; // 导出的表头名
const filterVal = _filterVal; // 导出的表头字段名
const data = formatJson(filterVal, _exData)