chenlu
2024-05-21 18fb477ea840e3dd4b19ff63f68f994d31fab43b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import request from "@/utils/request"
import {ElMessage} from "element-plus"
 
 
export default function exportExcel(url, fileName,date) {
    if(date===null){
        ElMessage.warning("请先选择日期")
        return
    }
 
    const date1 = new Date(date[0]);
    const date2 = new Date(date[1]);
    const timeDiff = Math.abs(date2.getTime() - date1.getTime());
    const daysDiff = timeDiff / (1000 * 3600 * 24);
    if(Math.floor(daysDiff)>180){
        ElMessage.warning("导出的筛选时间不能超过180天")
        return
    }
    request.post(url,date,{responseType :'blob'}).then(res => {
        const blob = new Blob([res])
        if ('download' in document.createElement('a')) { // 非IE下载
            const elink = document.createElement('a')
            elink.download = `${fileName}.xlsx`
            elink.style.display = 'none'
            elink.href = URL.createObjectURL(blob)
            document.body.appendChild(elink)
            elink.click()
            URL.revokeObjectURL(elink.href) // 释放URL 对象
            document.body.removeChild(elink)
        } else { // IE10+下载
            navigator.msSaveBlob(blob, fileName)
        }
 
    })
}