Excel导入导出

... 2021-7-29 Less than 1 minute

# Excel导入导出

# 代码

代码地址 (opens new window)

# 使用

  • 1.npm install avue-plugin-excel --save
  • 2.import $Excel from 'avue-plugin-excel'
  • 3.$Excel();
  • 4.参考如下代码使用
 <template>
  <div>
    <button id="button1"
            ref="button">导出excel</button>
    <input type="file"
           onchange="change(this)">
    <div style="height:30px"></div>
    <textarea id="text"
              class="text">
      [{
      "name": "张三",
      "sex": "男"
    }]
</textarea>
  </div>
</template>

<script>
import $Excel from 'avue-plugin-excel'
export default {
  mounted () {
    this.$nextTick(() => {
      var text = document.getElementById('text');
      var button1 = document.getElementById('button1')
      button1.onclick = () => {
        let opt = {
          title: '文档标题',
          column: [{
            label: '多级表头',
            prop: 'header',
            children: [
              {
                label: '姓名',
                prop: 'name'
              }, {
                label: '年龄',
                prop: 'sex'
              }
            ]
          }],
          data: JSON.parse(text.value, null, 4)
        }
        $Excel.excel({
          title: opt.title,
          columns: opt.column,
          data: opt.data
        });
      }
      function change (obj) {
        var files = obj.files[0];
        $Excel.xlsx(files)
          .then(data => {
            text.value = JSON.stringify(data.results, null, 4)
          })
      }
    })
  }
}
</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
Last update: August 20, 2021 09:25
Contributors: smallwei
您正在浏览基于 Vue 2.x 的 Avue 文档; 点击这里 查看 Vue 3.x 的升级版本