formDetail.vue 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <template>
  2. <div class="app-container p-2">
  3. <el-row :gutter="20">
  4. <el-col :lg="30" :xs="24" style="">
  5. <el-row :span="24" :gutter="10">
  6. <el-col :span="18" label="任务名称">
  7. <h2 v-if="detailData.title" key="business" style="font-weight: bolder">{{ detailData.title }}</h2>
  8. <p class="report-period">【填报周期】:{{ detailData.start }} 至 {{ detailData.end }}</p>
  9. </el-col>
  10. <el-col :span="1.5">
  11. <el-button type="primary" @click="exportToExcel()"> 导出表格 </el-button>
  12. </el-col>
  13. <el-col :span="1.5">
  14. <el-button type="danger" @click="handleReturn()"> 返回 </el-button>
  15. </el-col>
  16. </el-row>
  17. </el-col>
  18. <el-col :lg="30" :xs="24">
  19. <el-table :data="tableData" border>
  20. <el-table-column v-for="header in editableHeaders" :key="header" :label="header" :prop="header">
  21. <template #header="{ column }">
  22. <span class="editable-header" v-text="column.label"></span>
  23. </template>
  24. <template #default="{ row, $index }">
  25. <span class="editable-span" v-text="row[header]"></span>
  26. </template>
  27. </el-table-column>
  28. </el-table>
  29. </el-col>
  30. </el-row>
  31. </div>
  32. </template>
  33. <script setup lang="ts">
  34. import { ref, onMounted } from 'vue';
  35. import { ElTable, ElButton, ElCol, ElRow, ElTableColumn } from 'element-plus';
  36. import * as XLSX from 'xlsx';
  37. const emits = defineEmits(['close']);
  38. const detailData = ref({
  39. title: '测试表单',
  40. start: '2024-10-15 17:02:22',
  41. end: '2024-10-15 18:00:00'
  42. });
  43. const editableHeaders = ref(['时间', '地点', '损坏程度', '救援人员', '物资']);
  44. const tableData = ref([]);
  45. onMounted(() => {
  46. loadFromLocalStorage();
  47. addDefaultRow();
  48. });
  49. function loadFromLocalStorage() {
  50. const storedData = localStorage.getItem('tableData');
  51. if (storedData) {
  52. tableData.value = JSON.parse(storedData);
  53. } else {
  54. tableData.value = [
  55. {
  56. 时间: '2024-01-01',
  57. 地点: '某地',
  58. 损坏程度: '轻度',
  59. 救援人员: '张三, 李四',
  60. 物资: '食品, 水'
  61. },
  62. {
  63. 时间: '2024-01-02',
  64. 地点: '某地',
  65. 损坏程度: '中度',
  66. 救援人员: '王五, 赵六',
  67. 物资: '帐篷, 医疗用品'
  68. }
  69. ];
  70. }
  71. }
  72. function addDefaultRow() {
  73. tableData.value.push({
  74. 时间: '',
  75. 地点: '',
  76. 损坏程度: '',
  77. 救援人员: '',
  78. 物资: ''
  79. });
  80. }
  81. const exportToExcel = () => {
  82. const worksheet = XLSX.utils.json_to_sheet(tableData.value);
  83. const workbook = { Sheets: { data: worksheet }, SheetNames: ['data'] };
  84. const excelBuffer = XLSX.write(workbook, { bookType: 'xlsx', type: 'array' });
  85. const blob = new Blob([excelBuffer], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' });
  86. const link = document.createElement('a');
  87. const url = window.URL.createObjectURL(blob);
  88. link.href = url;
  89. link.download = 'SheetJS.xlsx';
  90. link.click();
  91. window.URL.revokeObjectURL(url); // 清理
  92. };
  93. const handleReturn = () => {
  94. emits('close');
  95. };
  96. </script>
  97. <style scoped>
  98. .app-container {
  99. font-family: Avenir, Helvetica, Arial, sans-serif;
  100. -webkit-font-smoothing: antialiased;
  101. -moz-osx-font-smoothing: grayscale;
  102. color: #2c3e50;
  103. }
  104. .report-period {
  105. margin-top: 10px;
  106. font-size: 14px;
  107. color: #606266;
  108. }
  109. .editable-span {
  110. cursor: default;
  111. }
  112. .editable-header {
  113. cursor: default;
  114. }
  115. </style>