point.vue 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <template>
  2. <div>
  3. <div class="top">
  4. <el-upload
  5. class="upload-demo"
  6. action="#"
  7. :show-file-list="false"
  8. :on-change="handleChange"
  9. >
  10. <el-button size="small" type="primary">点击上传</el-button>
  11. </el-upload>
  12. <el-select
  13. v-model="value"
  14. placeholder="请选择"
  15. filterable
  16. @change="byTableName"
  17. >
  18. <el-option
  19. v-for="item in tableList"
  20. :key="item.value"
  21. :label="item.label"
  22. :value="item.value"
  23. >
  24. </el-option>
  25. </el-select>
  26. <el-cascader
  27. v-model="value1"
  28. placeholder="字段"
  29. :options="options"
  30. :props="{ multiple: true }"
  31. collapse-tags
  32. filterable
  33. ></el-cascader>
  34. <el-button type="primary" @click="search" >查询</el-button>
  35. <download-excel
  36. class="export-excel-wrapper"
  37. :data="tableData"
  38. :fields="json_fields"
  39. :name="excelTitle"
  40. :disabled='disabled'
  41. >
  42. <!-- 上面可以自定义自己的样式,还可以引用其他组件button -->
  43. <el-button type="primary">导出EXCEL</el-button>
  44. </download-excel>
  45. </div>
  46. <el-table :data="tableData" style="width: 100%">
  47. <el-table-column
  48. v-for="i in arr"
  49. :key="i"
  50. :prop="i"
  51. :label="i"
  52. width="180"
  53. >
  54. </el-table-column>
  55. </el-table>
  56. </div>
  57. </template>
  58. <script>
  59. import XLSX from "xlsx";
  60. export default {
  61. data() {
  62. return {
  63. disabled:true,
  64. excelTitle:'' || 'excel',
  65. props: { multiple: true },
  66. tableData: [{}],
  67. excelData: [],
  68. arr: [],
  69. value: "",
  70. value1: "",
  71. tableList: [],
  72. options: [],
  73. json_fields: {},
  74. json_meta: [
  75. [
  76. {
  77. " key ": " charset ",
  78. " value ": " utf- 8 ",
  79. },
  80. ],
  81. ],
  82. };
  83. },
  84. created() {
  85. this.fetch();
  86. },
  87. mounted() {},
  88. methods: {
  89. fetch() {
  90. this.$http
  91. .get("UnionTable/tableNames")
  92. .then((res) => {
  93. var res = res.data;
  94. for (let i in res) {
  95. var obj = {};
  96. obj.lable = obj.value = res[i];
  97. this.tableList.push(obj);
  98. }
  99. })
  100. .catch(function (error) {
  101. console.log(error);
  102. });
  103. },
  104. byTableName() {
  105. this.options = [];
  106. this.$http
  107. .post("UnionTable/columns", this.value)
  108. .then((res) => {
  109. console.log(res);
  110. var res = res.data;
  111. for (let i in res) {
  112. var obj = {};
  113. obj.label = obj.value = res[i];
  114. console.log(obj);
  115. this.options.push(obj);
  116. }
  117. })
  118. .catch(function (error) {
  119. console.log(error);
  120. });
  121. },
  122. search() {
  123. this.$http
  124. .post("UnionTable/tableData", {
  125. tableName: this.value,
  126. columnsName: this.value1.flat(2),
  127. excelData: this.excelData,
  128. })
  129. .then((res) => {
  130. console.log(res.data);
  131. var json = res.data;
  132. var arr = [];
  133. for (let v in json) {
  134. if (v > 100) {
  135. break;
  136. }
  137. for (let k in Object.keys(json[v])) {
  138. if (arr.indexOf(Object.keys(json[v])[k]) == -1) {
  139. arr.push(Object.keys(json[v])[k]);
  140. }
  141. }
  142. }
  143. this.arr = arr;
  144. console.log(arr)
  145. var cluHead = {};
  146. for(let i in arr){
  147. cluHead[arr[i]] = arr[i]
  148. }
  149. console.log(cluHead)
  150. this.json_fields = cluHead
  151. this.$nextTick(() => {
  152. this.tableData = json;
  153. this.disabled = false
  154. });
  155. })
  156. .catch(function (error) {
  157. console.log(error);
  158. });
  159. },
  160. handleChange(file, fileList) {
  161. this.excelTitle = file.name
  162. const fileReader = new FileReader();
  163. fileReader.onload = (ev) => {
  164. try {
  165. const data = ev.target.result;
  166. const workbook = XLSX.read(data, {
  167. type: "binary",
  168. });
  169. let sheet = Object.keys(workbook.Sheets)[0];
  170. const json = XLSX.utils.sheet_to_json(workbook.Sheets[sheet]); //获得以第一列为键名的sheet数组对象
  171. this.$nextTick(() => {
  172. console.log(json);
  173. this.excelData = json;
  174. });
  175. } catch (e) {
  176. console.log(e);
  177. }
  178. };
  179. fileReader.readAsBinaryString(file.raw);
  180. },
  181. },
  182. };
  183. </script>
  184. <style lang="scss" scoped>
  185. .top {
  186. width: 60%;
  187. margin-top: 10px;
  188. display: flex;
  189. justify-content: space-around;
  190. overflow: hidden;
  191. }
  192. </style>