commonExport.vue 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <template>
  2. <span>
  3. <el-button
  4. type="primary"
  5. v-show="!exportLoading"
  6. @click="exportData"
  7. size="small"
  8. icon="el-icon-download"
  9. >导出
  10. </el-button>
  11. <el-button
  12. size="small"
  13. icon="el-icon-download"
  14. :loading="true"
  15. v-show="exportLoading"
  16. >导出数据中...
  17. </el-button>
  18. </span></template
  19. >
  20. <script>
  21. export default {
  22. props: ["form", "exportUrl", "exportFileName"],
  23. data() {
  24. return { exportLoading: false };
  25. },
  26. methods: {
  27. exportData() {
  28. if (!this.form.examId) {
  29. this.$notify({
  30. title: "警告",
  31. message: "请选择考试批次",
  32. type: "warning",
  33. duration: 1000
  34. });
  35. return false;
  36. }
  37. this.$confirm("确定执行导出?", "提示", {
  38. confirmButtonText: "确定",
  39. cancelButtonText: "取消",
  40. type: "warning"
  41. }).then(() => {
  42. this.exportLoading = true;
  43. this.$http
  44. .get(this.exportUrl, {
  45. params: {
  46. query: this.form
  47. },
  48. responseType: "arraybuffer",
  49. timeout: 20 * 60 * 1000 //限时20分钟
  50. })
  51. .then(response => {
  52. if (response.data) {
  53. var blob = new Blob([response.data], {
  54. type:
  55. "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
  56. });
  57. var url = URL.createObjectURL(blob);
  58. var a = document.createElement("a");
  59. a.href = url;
  60. a.download = this.exportFileName + ".xlsx";
  61. a.target = "_blank";
  62. a.click();
  63. URL.revokeObjectURL(url);
  64. }
  65. this.exportLoading = false;
  66. });
  67. });
  68. }
  69. }
  70. };
  71. </script>