exportExcel.vue 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <template>
  2. <div class="export-excel-container">
  3. <vab-query-form>
  4. <vab-query-form-left-panel :span="24">
  5. <el-form :inline="true" @submit.native.prevent>
  6. <el-form-item label="文件名">
  7. <el-input v-model="filename" placeholder="请输出要导出文件的名称" />
  8. </el-form-item>
  9. <el-form-item label="文件类型">
  10. <el-select v-model="bookType">
  11. <el-option
  12. v-for="item in options"
  13. :key="item"
  14. :label="item"
  15. :value="item"
  16. />
  17. </el-select>
  18. </el-form-item>
  19. <el-form-item label="自动列宽">
  20. <el-radio-group v-model="autoWidth">
  21. <el-radio :label="true">
  22. </el-radio>
  23. <el-radio :label="false">
  24. </el-radio>
  25. </el-radio-group>
  26. </el-form-item>
  27. <el-form-item>
  28. <el-button type="primary" @click="handleDownload">
  29. 导出 Excel
  30. </el-button>
  31. </el-form-item>
  32. </el-form>
  33. </vab-query-form-left-panel>
  34. </vab-query-form>
  35. <el-table
  36. v-loading="listLoading"
  37. :data="list"
  38. :element-loading-text="elementLoadingText"
  39. >
  40. <el-table-column show-overflow-tooltip label="序号" width="55">
  41. <template slot-scope="scope">
  42. {{ scope.$index + 1 }}
  43. </template>
  44. </el-table-column>
  45. <el-table-column show-overflow-tooltip label="标题">
  46. <template slot-scope="scope">
  47. {{ scope.row.title }}
  48. </template>
  49. </el-table-column>
  50. <el-table-column show-overflow-tooltip label="作者">
  51. <template slot-scope="scope">
  52. <el-tag>{{ scope.row.author }}</el-tag>
  53. </template>
  54. </el-table-column>
  55. <el-table-column show-overflow-tooltip label="访问量">
  56. <template slot-scope="scope">
  57. {{ scope.row.pageViews }}
  58. </template>
  59. </el-table-column>
  60. <el-table-column show-overflow-tooltip label="时间">
  61. <template slot-scope="scope">
  62. <span>{{ scope.row.datetime }}</span>
  63. </template>
  64. </el-table-column>
  65. </el-table>
  66. </div>
  67. </template>
  68. <script>
  69. import { getList } from "@/api/table";
  70. export default {
  71. name: "ExportExcel",
  72. data() {
  73. return {
  74. list: null,
  75. listLoading: true,
  76. downloadLoading: false,
  77. filename: "",
  78. autoWidth: true,
  79. bookType: "xlsx",
  80. elementLoadingText: "正在加载...",
  81. options: ["xlsx", "csv", "txt"],
  82. };
  83. },
  84. created() {
  85. this.fetchData();
  86. },
  87. methods: {
  88. async fetchData() {
  89. this.listLoading = true;
  90. const { data } = await getList();
  91. this.list = data;
  92. this.listLoading = false;
  93. },
  94. async handleDownload() {
  95. this.downloadLoading = true;
  96. const { export_json_to_excel } = await import("@/vendor/ExportExcel");
  97. const tHeader = ["Id", "Title", "Author", "Readings", "Date"];
  98. const filterVal = ["id", "title", "author", "pageViews", "datetime"];
  99. const list = this.list;
  100. const data = this.formatJson(filterVal, list);
  101. export_json_to_excel({
  102. header: tHeader,
  103. data,
  104. filename: this.filename,
  105. autoWidth: this.autoWidth,
  106. bookType: this.bookType,
  107. });
  108. this.downloadLoading = false;
  109. },
  110. formatJson(filterVal, jsonData) {
  111. return jsonData.map((v) =>
  112. filterVal.map((j) => {
  113. return v[j];
  114. })
  115. );
  116. },
  117. },
  118. };
  119. </script>