下载Excel表格封装组件
import { api} from '@/axios'
import { Message} from 'element-ui'
import store from '@/store/index.ts'
export function download ( obj: any , url: string , token: string , downLoadTitle: string ) { api. get ( url, { params: obj, responseType: 'blob' , header: { 'Authorization' : token} } ) . then ( ( res: any ) => { if ( res. data. type === 'application/json' ) { api. get ( url, { params: obj} ) . then ( ( ret: any ) => { Message. warning ( ret. data. data) } ) } else { let blob = new Blob ( [ res. data] , { type : 'application/vnd.ms-excel;charset=utf-8' } ) ; if ( res. headers[ 'refreshtoken' ] ) { store. commit ( 'refreshToken' , 'Bearer' + res. headers[ 'refreshtoken' ] ) } if ( 'download' in document. createElement ( 'a' ) ) { const downloadElement: any = document. createElement ( 'a' ) 继续; downloadElement. download = ` ${ downLoadTitle} ` . xlsx; downloadElement. style. display = 'none' ; downloadElement. href = URL . createObjectURL ( blob) ; document. body. appendChild ( downloadElement) ; downloadElement. click ( ) ; ( window. URL as any ) . revokeObjectURL ( downloadElement. href) ; document. body. removeChild ( downloadElement) ; } else { navigator. msSaveBlob ( blob, ` ${ downLoadTitle} ` . xlsx) } } } ) }
export default downLoad