ImportExcel.vue 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <template>
  2. <div class="form-body">
  3. <Row>
  4. <div class="ivu-upload-list-file" v-if="file !== null">
  5. <Icon type="ios-stats"></Icon>
  6. {{ file.name }}
  7. <Icon v-show="showRemoveFile" type="ios-close" class="ivu-upload-list-remove" @click.native="handleRemove()"></Icon>
  8. </div>
  9. </Row>
  10. <Row>
  11. <transition name="fade">
  12. <Progress v-if="showProgress" :percent="progressPercent" :stroke-width="2">
  13. <div v-if="progressPercent == 100">
  14. <Icon type="ios-checkmark-circle"></Icon>
  15. <span>成功</span>
  16. </div>
  17. </Progress>
  18. </transition>
  19. </Row>
  20. <Row>
  21. <Table :columns="tableTitle" :data="tableData" :loading="tableLoading" style="max-height:590px; overflow-y:scroll;"></Table>
  22. <div style="background-color:white; width:100%; height:50px;">
  23. <Upload action="" :before-upload="handleBeforeUpload" accept=".xls, .xlsx" style="display:block; float:right;">
  24. <Button :loading="uploadLoading" @click="openUploadFile" icon="ios-folder-open" class="btn">
  25. 打开文件
  26. </Button>
  27. <Button :loading="uploadLoading" @click.stop="sendUploadFile" icon="md-cloud-upload" class="btn">
  28. 上传文件
  29. </Button>
  30. </Upload>
  31. </div>
  32. </Row>
  33. </div>
  34. </template>
  35. <script>
  36. import excel from '../tools/excel.js'
  37. export default {
  38. data() {
  39. return {
  40. uploadLoading: false,
  41. progressPercent: 0,
  42. showProgress: false,
  43. showRemoveFile: false,
  44. file: null,
  45. tableData: [],
  46. tableTitle: [],
  47. tableLoading: false
  48. }
  49. },
  50. methods: {
  51. initUpload() {
  52. this.file = null
  53. this.showProgress = false
  54. this.loadingProgress = 0
  55. this.tableData = []
  56. this.tableTitle = []
  57. },
  58. openUploadFile() {
  59. this.initUpload()
  60. },
  61. handleRemove() {
  62. this.initUpload()
  63. this.$Message.info('打开的文件已删除!')
  64. },
  65. handleBeforeUpload(file) {
  66. const fileExt = file.name.split('.').pop().toLocaleLowerCase()
  67. if (fileExt === 'xlsx' || fileExt === 'xls') {
  68. this.readFile(file)
  69. this.file = file
  70. } else {
  71. this.$Notice.warning({
  72. title: '文件类型错误',
  73. desc: '文件:' + file.name + '不是EXCEL文件,请选择后缀为.xlsx或者.xls的EXCEL文件。'
  74. })
  75. }
  76. return false
  77. },
  78. // 读取文件
  79. readFile(file) {
  80. const reader = new FileReader();
  81. reader.readAsArrayBuffer(file);
  82. reader.onloadstart = e => {
  83. this.uploadLoading = true
  84. this.tableLoading = true
  85. this.showProgress = true
  86. }
  87. reader.onprogress = e => {
  88. this.progressPercent = Math.round(e.loaded / e.total * 100)
  89. }
  90. reader.onerror = e => {
  91. this.$Message.error('文件读取出错')
  92. }
  93. reader.onload = e => {
  94. this.$Message.info('文件读取成功');
  95. const data = e.target.result;
  96. const { header, results } = excel.read(data, 'array');
  97. const tableTitle = header.map(item => { return { title: item, key: item } });
  98. this.tableData = results;
  99. this.tableTitle = tableTitle;
  100. this.uploadLoading = false;
  101. this.tableLoading = false;
  102. this.showRemoveFile = true;
  103. console.log(this.tableData);
  104. }
  105. },
  106. sendUploadFile() {
  107. }
  108. },
  109. mounted() {
  110. }
  111. }
  112. </script>
  113. <style scoped>
  114. .btn {
  115. background: white;
  116. margin-top: 15px;
  117. font-size: 15px;
  118. padding: 5px 10px;
  119. width: 120px;
  120. padding: 4px 10px;
  121. margin-left: 10px;
  122. }
  123. </style>