1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
<template>
  <div class="zong" >
    <div style="display: flex;">
      <el-select
        v-model="value"
        placeholder="请选择检查设备"
        @change="getMonitor"
      >
        <el-option
          v-for="item in options"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        >
        </el-option>
      </el-select>
      <el-select
        v-model="value1"
        multiple
        collapse-tags
        style="margin-left: 20px"
        placeholder="请选择监测因子"
      >
        <el-option
          v-for="item in Monfactors"
          :key="item.value"
          :label="item.name"
          :value="item.code"
        >
        </el-option>
      </el-select>
      <el-date-picker
        v-model="startvalue"
        type="datetime"
        placeholder="选择开始日期时间"
        value-format="yyyy-MM-dd HH"
        style="margin-left: 20px"
      >
      </el-date-picker>
      <el-date-picker
        v-model="endvalue"
        type="datetime"
        placeholder="选择结束日期时间"
        value-format="yyyy-MM-dd HH"
        style="margin-left: 20px"
      >
      </el-date-picker>
      <el-button type="primary" @click="toExcel()" style="margin-left: 20px"
        >导出</el-button
      >
      <el-button @click="exportMon">查询</el-button>
    </div>
   
    <el-table
      :data="dateList"
      style="margin-top: 20px;"
      id="exportTab"
      border
       max-height="800"
    >
      <el-table-column
        v-for="item in columnList"
        :prop="item"
        :label="item"
        width="180px"
        :key="item"
      >
      </el-table-column>
    </el-table>
  </div>
</template>
<script>
//这里可以导入其他文件(比如:组件,工具js,第三方插件js,json文件,图片文件等等)
// 例如:import《组件名称》from'《组件路径》';
import FileSaver from 'file-saver'
import XLSX2 from 'xlsx-style'
import XLSX from 'xlsx'
export default {
  //import 引入的组件需要注入到对象中才能使用
  components: {},
  props: {},
  data() {
    //这里存放数据
    return {
      value: '',
      value1: [],
      startvalue: '',
      endvalue: '',
      options: [],
      columnList: [], //表头的数组
      dateList: [],
      newMac1:[],
      Monfactors: [],
      tableData: [],
      tableFields: {},
      json_meta: [
        [
          {
            key: 'charset',
            value: 'utf-8',
          },
        ],
      ],
    }
  },
  // 计算属性 类似于data概念
  computed: {},
  // 监控data中的数据变化
  watch: {
    // value(one,two){
    //   this.newMac1=[]
    //    for (let i = 0; i < one.length; i++) {
    //     // console.log(newVal[i], 111)
    //     this.newMac1.push(one[i][1][1])
    //   }
    //    this.getSensor()
    // }
  },
  //方法集合
  name: 'exporName',
  methods: {
    getMonitor(res){
      this.Monfactors=[]
      this.$request({
        url:'sensor/getSensorsByMonitorPointIds',
        method:'get',
        params:{
          monitorPointIds:res
        }
      }).then((res)=>{
        console.log(res);
        this.Monfactors=res.data
      })
    },
    exportMon() {
      if(this.value===""){
         this.$message.warning('请选择检查设备')
        return
      }
       if(this.value1===""){
         this.$message.warning('请选择因子')
        return
      }
       if(this.startvalue===""){
         this.$message.warning('请选择开始时间')
        return
      }
       if(this.endvalue===""){
         this.$message.warning('请选择结束时间')
        return
      }
      this.dateList = []
      this.columnList = []
      this.$request({
        url: '/monitorPoint/getHourlyDataByMonitorPoint',
        method: 'get',    
        params: {
          monitorPointId: this.value,
          sensors: this.value1.toString(),
          startTime: this.startvalue,
          endTime: this.endvalue,
        },
      }).then((res) => {
        console.log(res, 11)
        this.tableData = res.data
        console.log(this.tableData, 'this.tableData') 
        for (let key in this.tableData[0]) {       
          this.columnList.push(key)
        }
        for (let item of this.tableData) {
          this.dateList.push(item)
        }
      })
      console.log(1);
    },
    getMonitorPointId() {
      this.$request({
        url: '/monitorPoint/queryAllMonitorPoints',
        method: 'get',
        params: {
          organizationId: this.$store.state.orgId,
        },
      })
        .then((res) => {
          // console.log('请求站点的信息')
          // console.log(res)
          for (let i = 0; i < res.data.length; i++) {
            this.options.push({
              value: res.data[i].id,
              label: res.data[i].name,
            })
          }
        console.log(this.options);
 
        })
        .catch((err) => {
          console.log(err)
        })
        
    },
    toExcel() {
      let wb = XLSX.utils.table_to_book(document.querySelector('#exportTab'), { sheet: "分组表" });
      this.setExlStyle(wb["Sheets"]["分组表"]);
      var ws = XLSX2.write(wb, {
        type: "buffer",
      });
      try {
        FileSaver.saveAs(
          new Blob([ws], { type: "application/octet-stream" }),
          `监测站点数据表.xlsx`
        );
      } catch (e) {
        if (typeof console !== "undefined") console.log(e, ws);
      }
      return ws;  
    },
    setExlStyle(data) {
      let borderAll = {
        //单元格外侧框线
        top: {
          style: "thin",
        },
        bottom: {
          style: "thin",
        },
        left: {
          style: "thin",
        },
        right: {
          style: "thin",
        },
      };
      data["!cols"] = [];
      for (let key in data) {
        if (data[key] instanceof Object) {
          data[key].s = {
            border: borderAll,
            alignment: {
              horizontal: "center", //水平居中对齐
              vertical: "center",
            },
            font: {
              sz: 11,
            },
            bold: true,
            numFmt: 0,
          };
          data["!cols"].push({ wpx: 200 });
        }
      }
      return data;
    },
    
  },
  //生命周期 - 创建完成(可以访问当前 this 实例)
  created() {
    this.getMonitorPointId();
  },
  //生命周期 - 挂载完成(可以访问 DOM 元素)
  mounted() {},
  beforeCreate() {}, //生命周期 - 创建之前
  beforeMount() {}, //生命周期 - 挂载之前
  beforeUpdate() {}, //生命周期 - 更新之前
  updated() {}, //生命周期 - 更新之后
  beforeDestroy() {}, //生命周期 - 销毁之前
  destroyed() {}, //生命周期 - 销毁完成
  activated() {}, //如果页面有keep-alive缓存功能,这个函数会触发
}
</script>
<style scoped>
.zong {
  padding: 20px;
}
 
</style>