feat: 增加设备数据导出
This commit is contained in:
@@ -34,6 +34,14 @@
|
||||
style="width: 150px; margin-right: 10px"
|
||||
/>
|
||||
<el-button type="primary" icon="el-icon-search" size="small" style="margin-left: 20px" plain @click="pageSize = 20;pageCurrent = 1;searchTable()">查询</el-button>
|
||||
<el-button
|
||||
:disabled="tableData.length === 0"
|
||||
:loading="exporting"
|
||||
type="success"
|
||||
icon="el-icon-download"
|
||||
size="small"
|
||||
plain
|
||||
@click="exportToExcel">导出</el-button>
|
||||
</div>
|
||||
<div>
|
||||
<el-table :data="tableData" border size="small" stripe highlight-current-row height="740px" >
|
||||
@@ -88,6 +96,7 @@
|
||||
<script>
|
||||
import { mapGetters } from 'vuex'
|
||||
import { getNowDate, getNowTime } from '../../../utils/tool'
|
||||
import * as XLSX from 'xlsx'
|
||||
export default {
|
||||
data() {
|
||||
return {
|
||||
@@ -122,7 +131,8 @@ export default {
|
||||
tableData: [], // 设备信息表
|
||||
pageCurrent: 1, // 分页当前页
|
||||
pageSize: 20, // 分页每页显示条数
|
||||
dataname: ''
|
||||
dataname: '',
|
||||
exporting: false
|
||||
}
|
||||
},
|
||||
computed: {
|
||||
@@ -150,15 +160,7 @@ export default {
|
||||
})
|
||||
},
|
||||
searchTable() {
|
||||
var param = []
|
||||
param.push(['PageCurrent', this.pageCurrent]),
|
||||
param.push(['PageSize', this.pageSize]),
|
||||
param.push(['PageCount', '1111', 'int', '1']),
|
||||
param.push(['ItemCount', '1111', 'int', '1'])
|
||||
param.push(['开始时间', this.completionTime[0]])
|
||||
param.push(['结束时间', this.completionTime[1]])
|
||||
param.push(['设备名称', this.equipmentType])
|
||||
param.push(['参数名称', this.dataname])
|
||||
var param = this.buildQueryParams(this.pageSize, this.pageCurrent)
|
||||
var Data = this.CreateData('11', '设备管理_设备数据_历史查询', param)
|
||||
this.ExecDatabase(Data).then(response => {
|
||||
// this.tableData = response.data
|
||||
@@ -166,6 +168,73 @@ export default {
|
||||
this.total = parseInt(response.data.output[0].ItemCount)
|
||||
})
|
||||
},
|
||||
buildQueryParams(pageSize, pageCurrent) {
|
||||
var param = []
|
||||
param.push(['PageCurrent', pageCurrent])
|
||||
param.push(['PageSize', pageSize])
|
||||
param.push(['PageCount', '1111', 'int', '1'])
|
||||
param.push(['ItemCount', '1111', 'int', '1'])
|
||||
param.push(['开始时间', this.completionTime[0]])
|
||||
param.push(['结束时间', this.completionTime[1]])
|
||||
param.push(['设备名称', this.equipmentType])
|
||||
param.push(['参数名称', this.dataname])
|
||||
return param
|
||||
},
|
||||
exportToExcel() {
|
||||
if (this.tableData.length === 0) {
|
||||
this.$message.warning('没有数据可导出')
|
||||
return
|
||||
}
|
||||
|
||||
this.exporting = true
|
||||
const exportPageSize = this.total > 0 ? this.total : this.tableData.length
|
||||
const param = this.buildQueryParams(exportPageSize, 1)
|
||||
const Data = this.CreateData('11', '设备管理_设备数据_历史查询', param)
|
||||
|
||||
this.ExecDatabase(Data).then(response => {
|
||||
const rows = response.data && response.data.result ? response.data.result : []
|
||||
if (rows.length === 0) {
|
||||
this.$message.warning('没有数据可导出')
|
||||
return
|
||||
}
|
||||
this.writeHistoryExcel(rows)
|
||||
}).catch(error => {
|
||||
console.error('导出失败:', error)
|
||||
this.$message.error('导出失败')
|
||||
}).finally(() => {
|
||||
this.exporting = false
|
||||
})
|
||||
},
|
||||
writeHistoryExcel(rows) {
|
||||
const exportData = rows.map((item, index) => ({
|
||||
序号: index + 1,
|
||||
设备名称: item.设备代码 || '',
|
||||
参数名称: item.变量名称 || '',
|
||||
数值: item.值 || '',
|
||||
采集时间: item.创建时间 || ''
|
||||
}))
|
||||
const wb = XLSX.utils.book_new()
|
||||
const ws = XLSX.utils.json_to_sheet(exportData)
|
||||
ws['!cols'] = [
|
||||
{ wch: 8 },
|
||||
{ wch: 20 },
|
||||
{ wch: 24 },
|
||||
{ wch: 18 },
|
||||
{ wch: 22 }
|
||||
]
|
||||
XLSX.utils.book_append_sheet(wb, ws, '设备历史数据')
|
||||
XLSX.writeFile(wb, `设备历史数据_${this.formatDateForFileName(new Date())}.xlsx`)
|
||||
this.$message.success('导出成功')
|
||||
},
|
||||
formatDateForFileName(date) {
|
||||
const year = date.getFullYear()
|
||||
const month = String(date.getMonth() + 1).padStart(2, '0')
|
||||
const day = String(date.getDate()).padStart(2, '0')
|
||||
const hours = String(date.getHours()).padStart(2, '0')
|
||||
const minutes = String(date.getMinutes()).padStart(2, '0')
|
||||
const seconds = String(date.getSeconds()).padStart(2, '0')
|
||||
return `${year}${month}${day}_${hours}${minutes}${seconds}`
|
||||
},
|
||||
|
||||
handleSizeChanges(val) {
|
||||
this.pageCurrent = 1
|
||||
|
||||
@@ -21,6 +21,14 @@
|
||||
style="width: 150px; margin-right: 10px"
|
||||
/>
|
||||
<el-button type="primary" icon="el-icon-search" size="small" style="margin-left: 20px" plain @click="pageSize = 20;pageCurrent = 1;searchTable()">查询</el-button>
|
||||
<el-button
|
||||
:disabled="tableData.length === 0"
|
||||
:loading="exporting"
|
||||
type="success"
|
||||
icon="el-icon-download"
|
||||
size="small"
|
||||
plain
|
||||
@click="exportToExcel">导出</el-button>
|
||||
</div>
|
||||
<div>
|
||||
<el-table :data="tableData" border size="small" stripe highlight-current-row height="740px" >
|
||||
@@ -74,6 +82,7 @@
|
||||
|
||||
<script>
|
||||
import { mapGetters } from 'vuex'
|
||||
import * as XLSX from 'xlsx'
|
||||
export default {
|
||||
data() {
|
||||
return {
|
||||
@@ -107,7 +116,8 @@ export default {
|
||||
tableData: [], // 设备信息表
|
||||
pageCurrent: 1, // 分页当前页
|
||||
pageSize: 20, // 分页每页显示条数
|
||||
dataname: ''
|
||||
dataname: '',
|
||||
exporting: false
|
||||
}
|
||||
},
|
||||
computed: {
|
||||
@@ -148,6 +158,51 @@ export default {
|
||||
// this.total = parseInt(response.data.output[0].ItemCount)
|
||||
})
|
||||
},
|
||||
exportToExcel() {
|
||||
if (this.tableData.length === 0) {
|
||||
this.$message.warning('没有数据可导出')
|
||||
return
|
||||
}
|
||||
|
||||
this.exporting = true
|
||||
try {
|
||||
const exportData = this.tableData.map((item, index) => ({
|
||||
序号: index + 1,
|
||||
设备名称: item.Zone || '',
|
||||
参数名称: item.TagName || '',
|
||||
数值: item.TagValue || '',
|
||||
单位: item.TagUnit || '',
|
||||
更新时间: item.UpdateTime || ''
|
||||
}))
|
||||
const wb = XLSX.utils.book_new()
|
||||
const ws = XLSX.utils.json_to_sheet(exportData)
|
||||
ws['!cols'] = [
|
||||
{ wch: 8 },
|
||||
{ wch: 20 },
|
||||
{ wch: 24 },
|
||||
{ wch: 18 },
|
||||
{ wch: 12 },
|
||||
{ wch: 22 }
|
||||
]
|
||||
XLSX.utils.book_append_sheet(wb, ws, '设备实时数据')
|
||||
XLSX.writeFile(wb, `设备实时数据_${this.formatDateForFileName(new Date())}.xlsx`)
|
||||
this.$message.success('导出成功')
|
||||
} catch (error) {
|
||||
console.error('导出失败:', error)
|
||||
this.$message.error('导出失败')
|
||||
} finally {
|
||||
this.exporting = false
|
||||
}
|
||||
},
|
||||
formatDateForFileName(date) {
|
||||
const year = date.getFullYear()
|
||||
const month = String(date.getMonth() + 1).padStart(2, '0')
|
||||
const day = String(date.getDate()).padStart(2, '0')
|
||||
const hours = String(date.getHours()).padStart(2, '0')
|
||||
const minutes = String(date.getMinutes()).padStart(2, '0')
|
||||
const seconds = String(date.getSeconds()).padStart(2, '0')
|
||||
return `${year}${month}${day}_${hours}${minutes}${seconds}`
|
||||
},
|
||||
|
||||
handleSizeChanges(val) {
|
||||
this.pageCurrent = 1
|
||||
|
||||
Reference in New Issue
Block a user