export-excel.vue 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <template>
  2. <div class="app-container">
  3. <div>
  4. <FilenameOption v-model="filename" />
  5. <AutoWidthOption v-model="autoWidth" />
  6. <BookTypeOption v-model="bookType" />
  7. <el-button :loading="downloadLoading" style="margin:0 0 20px 20px;" type="primary" icon="el-icon-document" @click="handleDownload">
  8. Export Excel
  9. </el-button>
  10. <a href="https://panjiachen.github.io/vue-element-admin-site/feature/component/excel.html" target="_blank" style="margin-left:15px;">
  11. <el-tag type="info">Documentation</el-tag>
  12. </a>
  13. </div>
  14. <el-table v-loading="listLoading" :data="list" element-loading-text="Loading..." border fit highlight-current-row>
  15. <el-table-column align="center" label="Id" width="95">
  16. <template slot-scope="scope">
  17. {{ scope.$index }}
  18. </template>
  19. </el-table-column>
  20. <el-table-column label="Title">
  21. <template slot-scope="scope">
  22. {{ scope.row.title }}
  23. </template>
  24. </el-table-column>
  25. <el-table-column label="Author" width="110" align="center">
  26. <template slot-scope="scope">
  27. <el-tag>{{ scope.row.author }}</el-tag>
  28. </template>
  29. </el-table-column>
  30. <el-table-column label="Readings" width="115" align="center">
  31. <template slot-scope="scope">
  32. {{ scope.row.pageviews }}
  33. </template>
  34. </el-table-column>
  35. <el-table-column align="center" label="Date" width="220">
  36. <template slot-scope="scope">
  37. <i class="el-icon-time" />
  38. <span>{{ scope.row.timestamp | parseTime('{y}-{m}-{d} {h}:{i}') }}</span>
  39. </template>
  40. </el-table-column>
  41. </el-table>
  42. </div>
  43. </template>
  44. <script>
  45. import { fetchList } from '@/api/article'
  46. import { parseTime } from '@/utils'
  47. // options components
  48. import FilenameOption from './components/FilenameOption'
  49. import AutoWidthOption from './components/AutoWidthOption'
  50. import BookTypeOption from './components/BookTypeOption'
  51. export default {
  52. name: 'ExportExcel',
  53. components: { FilenameOption, AutoWidthOption, BookTypeOption },
  54. data() {
  55. return {
  56. list: null,
  57. listLoading: true,
  58. downloadLoading: false,
  59. filename: '',
  60. autoWidth: true,
  61. bookType: 'xlsx'
  62. }
  63. },
  64. created() {
  65. this.fetchData()
  66. },
  67. methods: {
  68. fetchData() {
  69. this.listLoading = true
  70. fetchList().then(response => {
  71. this.list = response.data.items
  72. this.listLoading = false
  73. })
  74. },
  75. handleDownload() {
  76. this.downloadLoading = true
  77. import('@/vendor/Export2Excel').then(excel => {
  78. const tHeader = ['Id', 'Title', 'Author', 'Readings', 'Date']
  79. const filterVal = ['id', 'title', 'author', 'pageviews', 'display_time']
  80. const list = this.list
  81. const data = this.formatJson(filterVal, list)
  82. excel.export_json_to_excel({
  83. header: tHeader,
  84. data,
  85. filename: this.filename,
  86. autoWidth: this.autoWidth,
  87. bookType: this.bookType
  88. })
  89. this.downloadLoading = false
  90. })
  91. },
  92. formatJson(filterVal, jsonData) {
  93. return jsonData.map(v => filterVal.map(j => {
  94. if (j === 'timestamp') {
  95. return parseTime(v[j])
  96. } else {
  97. return v[j]
  98. }
  99. }))
  100. }
  101. }
  102. }
  103. </script>
  104. <style>
  105. .radio-label {
  106. font-size: 14px;
  107. color: #606266;
  108. line-height: 40px;
  109. padding: 0 12px 0 30px;
  110. }
  111. </style>