参考https://blog.csdn.net/sj1527382290/article/details/104700416/

    如果封装了axios,请在需要导出的页面引入axios,然后直接使用axios进行使用

    1. import axios from 'axios';
    2. daoChuTableData() {
    3. // this.$R.daoChuZhiLiangBaobiao().then((res) => {
    4. // console.log("res :>> ", res);
    5. // downloadFile(res);
    6. // });
    7. axios({
    8. method: "post",
    9. url: '/clinbrain/api/compare/analysis/export',
    10. data: {},
    11. responseType: "blob" // 加上这个是重点
    12. }).then(res => {
    13. console.log('导出数据 :>> ', res);
    14. downloadFile(res.data);
    15. })
    16. function downloadFile(data) {
    17. let url = window.URL.createObjectURL(new Blob([data]));
    18. let link = document.createElement("a");
    19. let fileName = `导出.xls`;
    20. link.style.display = "none";
    21. link.href = url;
    22. link.setAttribute("download", fileName);
    23. document.body.appendChild(link);
    24. link.click();
    25. URL.revokeObjectURL(link.href);
    26. document.body.removeChild(link);
    27. }
    28. },

    1.vue + element ui 的 get 方法导出数据

    1. let params = JSON.parse(JSON.stringify(this.filterForm));
    2. let exportUrl = "";
    3. // 清除为空的字段
    4. for (const key in params) {
    5. if(params[key] !== '') {
    6. exportUrl += "&" + key + "=" + params[key];
    7. }
    8. }
    9. window.open('/add/' + exportUrl.slice(1))
    10. 1
    11. 2
    12. 3
    13. 4
    14. 5
    15. 6
    16. 7
    17. 8
    18. 9
    1. vue + element ui 的 post 方法导出数据

      1. this.$axios({
      2. method: "post",
      3. url: '接口地址',
      4. data: this.ruleForm,
      5. responseType: "blob" // 加上这个是重点
      6. }).then(res => {
      7. this.downloadFile(res.data);
      8. })
      9. downloadFile(data) {
      10. let url = window.URL.createObjectURL(new Blob([data]));
      11. let link = document.createElement("a");
      12. let fileName = `导出${utils.getTimeString(new Date())}.xls`;
      13. link.style.display = "none";
      14. link.href = url;
      15. link.setAttribute("download", fileName);
      16. document.body.appendChild(link);
      17. link.click();
      18. URL.revokeObjectURL(link.href);
      19. document.body.removeChild(link);
      20. this.$message({
      21. message: "导出成功",
      22. type: "success"
      23. });
      24. },
      25. 1
      26. 2
      27. 3
      28. 4
      29. 5
      30. 6
      31. 7
      32. 8
      33. 9
      34. 10
      35. 11
      36. 12
      37. 13
      38. 14
      39. 15
      40. 16
      41. 17
      42. 18
      43. 19
      44. 20
      45. 21
      46. 22
      47. 23
      48. 24
      49. 25
    2. vue + element ui 直接将文件传给后台

      1. <template>
      2. <div>
      3. <el-button type="primary" @click="upLoadHandle" icon="el-icon-upload">批量导入</el-button>
      4. <a :href="downUrl" style="display: none;" ref="downNode" target="_blank"></a>
      5. <input type="file" ref="upLoad" style="display: none;" @change="fileChangeHandle" />
      6. </div>
      7. </template>
      8. <script>
      9. methods: {
      10. upLoadHandle() {
      11. this.$refs.upLoad.value = ""; // 每次点击清空上次的值
      12. this.$refs.upLoad.click();
      13. },
      14. fileChangeHandle() {
      15. const file = this.$refs.upLoad.files[0];
      16. if (file) {
      17. let param = new FormData();
      18. param.append("excelFile", file);
      19. param.append("type", this.type);
      20. this.$axios.post("/add", param).then(res => {});
      21. }
      22. }
      23. }
      24. </script>
      25. • 1
      26. • 2
      27. • 3
      28. • 4
      29. • 5
      30. • 6
      31. • 7
      32. • 8
      33. • 9
      34. • 10
      35. • 11
      36. • 12
      37. • 13
      38. • 14
      39. • 15
      40. • 16
      41. • 17
      42. • 18
      43. • 19
      44. • 20
      45. • 21
      46. • 22
      47. • 23
      48. • 24

      4.vue + element ui 打开链接下载

      1. downExcelHandle(val) {
      2. if ( val.fileUrl == '') {
      3. this.$message({
      4. message: '链接为空,请联系后台人员',
      5. type: 'warning'
      6. });
      7. return
      8. }
      9. let downnode = document.createElement('a')
      10. let href = val.fileUrl
      11. downnode.href = href
      12. // 页面div的 id
      13. document.getElementById('serach').appendChild(downnode)
      14. downnode.click()
      15. },