# 三.文件处理(导入导出)

前言

主要介绍 excel 导入导出等常用的方法

# 1.表格导入

刷新
全屏/自适应

# 2.表格导出

:::

<template>
  <div>
    <iframe id="ifile" style="display: none"></iframe>
    <button
      type="primary"
      size="mini"
      style="margin-left: auto"
      @click="export"
    >
      导出
    </button>
  </div>
</template>
<script>
export default {
         getList() {

    let params={}
    this.$api.xxx.getData(params).then((res) => {
        if (res.data.status == 200) {
          this.exportList = JSON.parse(JSON.stringify(res.data.data));

        } else {

        }
  },
    data() {
        return {
            exportList:[]//导出列表
             form:{},
             createTimeValue:"",

        }
    },
    methods:{
             //导出
  exportAll(){
    if (this.exportList.length == 0) {
      return this.$message.warning({
        message: "没有数据!",
        center: true,
        type: "warning",
        showClose: true,
        duration: 1000,
      });
    }
   /*可选择时间
     else {
      if (this.createTimeValue) {
        if (this.createTimeValue[1] == this.createTimeValue[0]) {
          this.form.endTime = this.createTimeValue[1] + " 23:59:59";
          this.form.startTime = this.createTimeValue[0] + " 00:00:00";
        } else {
          this.form.endTime = this.createTimeValue[1] + " 23:59:59";
          this.form.startTime = this.createTimeValue[0] + " 00:00:00";
        }
      } else {
        this.form.endTime = "";
        this.form.startTime = "";
      }*/
      let json = { ...this.form};
      var time = new Date()
      axios({
        method: "post",
        url: base.baseURL + `file/export?startTime=${this.form.startTime}&endTime=${this.form.endTime}`,
        data: json,
        responseType: "arraybuffer", //要加responseType: 'arraybuffer'参数,不然下载的excel会乱码
      }).then((res) => {
        console.log(res)
          var blob = new Blob([res.data], {
            type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
          });
          var objectUrl = URL.createObjectURL(blob);
          var a = document.createElement("a");
          document.body.appendChild(a);
          a.style = "display: none";
          a.href = objectUrl;
          a.download = "下载文件01_"+time.toLocaleString().split('/').join('').split(':').join('').replace(/\s/g,"")+".xlsx";
          a.click();
          document.body.removeChild(a);
      })
        .catch((error) => {
          console.log(error);
          this.$message.warning({
            message: "error",
            center: true,
            type: "warning",
            showClose: true,
            duration: 1000,
          });
        });
    }
  },

    }
}
</script>
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97

:::