This commit is contained in:
chenjianan
2019-06-02 10:18:36 +08:00
8 changed files with 673 additions and 38 deletions

View File

@@ -0,0 +1,224 @@
/* eslint-disable */
require('script-loader!file-saver');
// import XLSX from 'xlsx'
import XLSX from 'xlsx-style'
function generateArray(table) {
var out = [];
var rows = table.querySelectorAll('tr');
var ranges = [];
for (var R = 0; R < rows.length; ++R) {
var outRow = [];
var row = rows[R];
var columns = row.querySelectorAll('td');
for (var C = 0; C < columns.length; ++C) {
var cell = columns[C];
var colspan = cell.getAttribute('colspan');
var rowspan = cell.getAttribute('rowspan');
var cellValue = cell.innerText;
if (cellValue !== "" && cellValue == +cellValue) cellValue = +cellValue;
//Skip ranges
ranges.forEach(function (range) {
if (R >= range.s.r && R <= range.e.r && outRow.length >= range.s.c && outRow.length <= range.e.c) {
for (var i = 0; i <= range.e.c - range.s.c; ++i) outRow.push(null);
}
});
//Handle Row Span
if (rowspan || colspan) {
rowspan = rowspan || 1;
colspan = colspan || 1;
ranges.push({
s: {
r: R,
c: outRow.length
},
e: {
r: R + rowspan - 1,
c: outRow.length + colspan - 1
}
});
};
//Handle Value
outRow.push(cellValue !== "" ? cellValue : null);
//Handle Colspan
if (colspan)
for (var k = 0; k < colspan - 1; ++k) outRow.push(null);
}
out.push(outRow);
}
return [out, ranges];
};
function datenum(v, date1904) {
if (date1904) v += 1462;
var epoch = Date.parse(v);
return (epoch - new Date(Date.UTC(1899, 11, 30))) / (24 * 60 * 60 * 1000);
}
function sheet_from_array_of_arrays(data, opts) {
var ws = {};
var range = {
s: {
c: 10000000,
r: 10000000
},
e: {
c: 0,
r: 0
}
};
for (var R = 0; R != data.length; ++R) {
for (var C = 0; C != data[R].length; ++C) {
if (range.s.r > R) range.s.r = R;
if (range.s.c > C) range.s.c = C;
if (range.e.r < R) range.e.r = R;
if (range.e.c < C) range.e.c = C;
var cell = {
v: data[R][C]
};
if (cell.v == null) continue;
var cell_ref = XLSX.utils.encode_cell({
c: C,
r: R
});
if (typeof cell.v === 'number') cell.t = 'n';
else if (typeof cell.v === 'boolean') cell.t = 'b';
else if (cell.v instanceof Date) {
cell.t = 'n';
cell.z = XLSX.SSF._table[14];
cell.v = datenum(cell.v);
} else cell.t = 's';
ws[cell_ref] = cell;
}
}
if (range.s.c < 10000000) ws['!ref'] = XLSX.utils.encode_range(range);
return ws;
}
function Workbook() {
if (!(this instanceof Workbook)) return new Workbook();
this.SheetNames = [];
this.Sheets = {};
}
function s2ab(s) {
var buf = new ArrayBuffer(s.length);
var view = new Uint8Array(buf);
for (var i = 0; i != s.length; ++i) view[i] = s.charCodeAt(i) & 0xFF;
return buf;
}
export function export_table_to_excel(id) {
var theTable = document.getElementById(id);
var oo = generateArray(theTable);
var ranges = oo[1];
/* original data */
var data = oo[0];
var ws_name = "SheetJS";
var wb = new Workbook(),
ws = sheet_from_array_of_arrays(data);
/* add ranges to worksheet */
// ws['!cols'] = ['apple', 'banan'];
ws['!merges'] = ranges;
/* add worksheet to workbook */
wb.SheetNames.push(ws_name);
wb.Sheets[ws_name] = ws;
var wbout = XLSX.write(wb, {
bookType: 'xlsx',
bookSST: false,
type: 'binary'
});
saveAs(new Blob([s2ab(wbout)], {
type: "application/octet-stream"
}), "test.xlsx")
}
export function export_json_to_excel({
headerGroup,
dataGroup,
sheetGroup,
filename,
autoWidth = true,
bookType= 'xlsx'
} = {}) {
var wb = new Workbook()
for (let i = 0; i < dataGroup.length; i++) {
/* original data */
let data = dataGroup[i]
filename = filename || 'excel-list'
data = [...data]
data.unshift(headerGroup[i]);
var ws_name = sheetGroup[i];
var ws = sheet_from_array_of_arrays(data);
if (autoWidth) {
/*设置worksheet每列的最大宽度*/
const colWidth = data.map(row => row.map(val => {
/*先判断是否为null/undefined*/
if (val == null) {
return {
'wch': 10
};
}
/*再判断是否为中文*/
else if (val.toString().charCodeAt(0) > 255) {
return {
'wch': val.toString().length * 2
};
} else {
return {
'wch': val.toString().length
};
}
}))
/*以第一行为初始值*/
let result = colWidth[0];
for (let i = 1; i < colWidth.length; i++) {
for (let j = 0; j < colWidth[i].length; j++) {
if (result[j]['wch'] < colWidth[i][j]['wch']) {
result[j]['wch'] = colWidth[i][j]['wch'];
}
}
}
ws['!cols'] = result;
}
let R = 0;
data.map(item => {
let C = 0;
item.map(itm => {
var cell_ref = XLSX.utils.encode_cell({c:C,r:R});
var cell = {v: itm }
cell.s= {
alignment: {
horizontal: "center"
}
}
ws[cell_ref] = cell;
C++;
})
R++;
})
/* add worksheet to workbook */
wb.SheetNames.push(ws_name);
wb.Sheets[ws_name] = ws;
}
var wbout = XLSX.write(wb, {
bookType: bookType,
bookSST: false,
type: 'binary'
});
saveAs(new Blob([s2ab(wbout)], {
type: "application/octet-stream"
}), `${filename}.${bookType}`);
}

View File

@@ -0,0 +1,24 @@
// 导出Excel方法表格id不加扩展名的文件名sheet名
export function exportExcelMethod(tableId, fileName, sheetName) {
tableToExcel(tableId, fileName, sheetName)
}
const tableToExcel = (function() {
const uri = 'data:application/vnd.ms-excel;base64,'
// 设置导出表格的单元格默认高度/宽度/边框样式/字体颜色/背景颜色/居中网页显示表格宽度建议1240tr/td视情况而定
const template = `<html xmlns:x="urn:schemas-microsoft-com:office:excel"><head><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><meta charset="UTF-8"><style type="text/css">table td {border: 1px solid #000000;height:40px;text-align: center;color: #000000;}</style></head><body><table>{table}</table></body></html>`
const base64 = function(s) { return window.btoa(unescape(encodeURIComponent(s))) }
const format = function(s, c) { return s.replace(/{(\w+)}/g, function(m, p) { return c[p] }) }
return function(table, filename, sheetname) {
if (!table.nodeType) table = document.getElementById(table)
const ctx = { worksheet: sheetname || 'Worksheet', table: table.innerHTML }
const blob = new Blob([format(template, ctx)])
if ('msSaveOrOpenBlob' in navigator) {
window.navigator.msSaveOrOpenBlob(blob, filename + '.xls')
return
}
const aTag = document.createElement('a')
aTag.href = uri + base64(format(template, ctx))
aTag.download = filename
aTag.click()
}
})()

View File

@@ -22,11 +22,12 @@
type="date"
placeholder="选择日期"/>
<el-button type="success" class="el-icon-search" size="small" style="margin: 10px 0 0 10px" @click="pageCurrent=1;total = 0;searchTable();">查询</el-button>
<el-button type="primary" size="small" icon="el-icon-download" style="margin-left:10px" @click="exportUninquirySheet1()">导出</el-button>
</el-row>
</el-card>
<el-card>
<el-col :span="6">
<el-table v-loading="loading" :data="tableData" highlight-current-row height="580" style="width: 100%;" border element-loading-text="拼命加载中" @row-click="searchTable2">
<el-table v-loading="loading" :data="tableData" highlight-current-row height="500" style="width: 100%;" border element-loading-text="拼命加载中" @row-click="searchTable2">
<el-table-column label="操作者" align="center" width="110px">
<template slot-scope="scope">
{{ scope.row.姓名 }}
@@ -55,7 +56,7 @@
</div>
</el-col>
<el-col :span="17" style="margin-left: 30px;">
<el-table v-loading="loading2" :data="tableData2" height="580" style="width: 100%;" border element-loading-text="拼命加载中">
<el-table v-loading="loading2" :data="tableData2" height="500" style="width: 100%;" border element-loading-text="拼命加载中">
<el-table-column label="零件图号" align="center" width="150px">
<template slot-scope="scope">
{{ scope.row.零件图号 }}
@@ -104,10 +105,90 @@
</div>
</el-col>
</el-card>
<el-card>
<span>机床编号:</span>
<el-select v-model="machineNumberValue" filterable placeholder="机床编号" size="small" clearable @change="searchTable4();searchTable6()">
<el-option
v-for="item in machineNumber"
:key="item.value"
:label="item.label"
:value="item.value">
<div style="float: left">{{ item.label }}</div>
<div style="float: right; color: #8492a6; font-size: 13px">{{ item.name }}</div>
</el-option>
</el-select>
<el-button type="primary" size="small" icon="el-icon-download" style="margin-left:10px" @click="exportUninquirySheet2()">机床导出</el-button>
<el-button type="primary" size="small" icon="el-icon-download" style="margin-left:10px" @click="exportUninquirySheet3()">组件导出</el-button>
</el-card>
<el-card>
<el-col :span="6">
<el-table v-loading="loading3" :data="tableData3" highlight-current-row height="580" style="width: 100%;" border element-loading-text="拼命加载中" @row-click="searchTable5">
<el-table-column label="机床编号" align="center" width="110px">
<template slot-scope="scope">
{{ scope.row.机床图号 }}
</template>
</el-table-column>
<el-table-column label="理论工时" align="center">
<template slot-scope="scope">
{{ scope.row.理论加工时间段 }}
</template>
</el-table-column>
<el-table-column label="实际工时" align="center">
<template slot-scope="scope">
{{ scope.row.实际加工时间段 }}
</template>
</el-table-column>
</el-table>
<div style="margin: 10px">
<el-pagination
v-if="!loading3"
:current-page="pageCurrent3"
:page-size="pageSize3"
:total="total3"
layout="total, prev, pager, next"
@size-change="handleSizeChange3"
@current-change="handleCurrentChange3"/>
</div>
</el-col>
<el-col :span="17" style="margin-left: 30px;">
<el-table v-loading="loading4" :data="tableData4" height="580" style="width: 100%;" border element-loading-text="拼命加载中">
<el-table-column label="机床编号" align="center">
<template slot-scope="scope">
{{ scope.row.机床图号 }}
</template>
</el-table-column>
<el-table-column label="组号" align="center">
<template slot-scope="scope">
{{ scope.row.组号 }}
</template>
</el-table-column>
<el-table-column label="理论工时" align="center">
<template slot-scope="scope">
{{ scope.row.理论加工时间段 }}
</template>
</el-table-column>
<el-table-column label="实际工时" align="center" width="120px">
<template slot-scope="scope">
{{ scope.row.实际加工时间段 }}
</template>
</el-table-column>
</el-table>
<div style="margin: 10px">
<el-pagination
v-if="!loading4"
:current-page="pageCurrent4"
:page-size="pageSize4"
:total="total4"
layout="total, prev, pager, next, jumper"
@size-change="handleSizeChange4"
@current-change="handleCurrentChange4"/>
</div>
</el-col>
</el-card>
</div>
</template>
<script>
import { searchtable, searchtable2 } from '@/api/ManufacturingCenter/machiningHoursStatistics'
import { searchtable, searchtable2, searchtable4, searchtable5, initProject, searchtable55, searchtable11, searchtable44 } from '@/api/ManufacturingCenter/machiningHoursStatistics'
export default {
data() {
@@ -118,20 +199,135 @@ export default {
loading: false,
tableData: [], // 表格数据
total: null, // 总条数
pageSize: 20, // 每页显示条数
pageSize: 10, // 每页显示条数
pageCurrent: 1, // 后台获取页
gongzuozhebianhao: '',
machinenumber: '',
personname: '',
machineNumberValue: '',
machineNumber: [],
loading2: false,
tableData2: [], // 表格数据
total2: null, // 总条数
pageSize2: 20, // 每页显示条数
pageCurrent2: 1 // 后台获取页
pageSize2: 10, // 每页显示条数
pageCurrent2: 1, // 后台获取页
loading3: false,
tableData3: [], // 表格数据
total3: null, // 总条数
pageSize3: 10, // 每页显示条数
pageCurrent3: 1, // 后台获取页
loading4: false,
tableData4: [], // 表格数据
total4: null, // 总条数
pageSize4: 10, // 每页显示条数
pageCurrent4: 1 // 后台获取页
}
},
created() {
this.initProject()
this.searchTable()
this.searchTable4()
},
methods: {
// 导出
exportUninquirySheet3() {
searchtable55(this.machineNumberValue).then(res1 => {
if (res1.data.length === 0) {
this.$message.error('请选择机床')
return
}
import('./Export2Excel').then(excel => {
const filterVal1 = ['机床图号', '组号', '理论加工时间段', '实际加工时间段'] // tableData1里的属性名
const data1 = res1.data.map(v => filterVal1.map(j => v[j]))
const dataGroup = []
dataGroup.push(data1)
const headerGroup = [['机床编号', '组号', '理论工时', '实际工时']]
const sheetGroup = ['工时明细表']
excel.export_json_to_excel({
headerGroup: headerGroup,
dataGroup: dataGroup,
sheetGroup: sheetGroup,
filename: res1.data[0]['机床图号'] + '_工时明细表',
autoWidth: true,
bookType: 'xlsx'
})
})
})
},
// 导出
exportUninquirySheet2() {
if (this.machineNumberValue !== '' && this.machineNumberValue !== null) { this.check1 = 1 } else { this.check1 = 0 }
searchtable44(0, this.machineNumberValue).then(res1 => {
if (res1.data.length === 0) {
this.$message.error('请选择机床')
return
}
import('./Export2Excel').then(excel => {
const filterVal1 = ['机床图号', '理论加工时间段', '实际加工时间段'] // tableData1里的属性名
const data1 = res1.data.map(v => filterVal1.map(j => v[j]))
const dataGroup = []
dataGroup.push(data1)
const headerGroup = [['机床编号', '理论工时', '实际工时']]
const sheetGroup = ['工时明细表']
excel.export_json_to_excel({
headerGroup: headerGroup,
dataGroup: dataGroup,
sheetGroup: sheetGroup,
filename: '机床工时明细表',
autoWidth: true,
bookType: 'xlsx'
})
})
})
},
// 导出
exportUninquirySheet1() {
if (this.startTime !== '' && this.startTime !== null && this.endTime !== null && this.endTime !== null) {
this.timeCheck = 1
} else {
this.startTime = ''
this.endTime = ''
this.timeCheck = 0
}
if (this.workerName !== '' && this.workerName !== null) {
this.workerName_ckeck = 1
} else {
this.workerName_ckeck = 0
}
searchtable11(this.workerName_ckeck, this.workerName, this.timeCheck, this.startTime, this.endTime).then(res1 => {
if (res1.data.length === 0) {
this.$message.error('请选择人员')
return
}
import('./Export2Excel').then(excel => {
const filterVal1 = ['姓名', '理论加工时间段', '实际加工时间段'] // tableData1里的属性名
const data1 = res1.data.map(v => filterVal1.map(j => v[j]))
const dataGroup = []
dataGroup.push(data1)
const headerGroup = [['操作者', '理论工时', '实际工时']]
const sheetGroup = ['人员工时明细表']
excel.export_json_to_excel({
headerGroup: headerGroup,
dataGroup: dataGroup,
sheetGroup: sheetGroup,
filename: '工时明细表',
autoWidth: true,
bookType: 'xlsx'
})
})
})
},
initProject() {
initProject().then(response => {
for (let i = 0; i < response.data.length; i++) {
this.machineNumber.push({
label: response.data[i].机床图号,
name: response.data[i].项目名称,
value: response.data[i].机床流水号
})
}
})
},
// 查询表格数据
searchTable() {
this.loading = true
@@ -166,11 +362,11 @@ export default {
this.tableData2 = []
this.pageCurrent2 = 1
this.gongzuozhebianhao = row.人员编号
this.personname = row.姓名
searchtable2(row.人员编号, this.timeCheck, this.startTime, this.endTime, this.pageCurrent2, this.pageSize2).then(response => {
if (response.data.total === 0) {
this.loading2 = false
} else {
console.log(response.data)
this.total2 = response.data.total
this.tableData2 = response.data.rows
}
@@ -192,6 +388,54 @@ export default {
this.loading2 = false
})
},
// 查询表格数据
searchTable4() {
this.loading3 = true
this.tableData3 = []
this.tableData4 = []
this.total3 = 0
if (this.machineNumberValue !== '' && this.machineNumberValue !== null) { this.check1 = 1 } else { this.check1 = 0 }
searchtable4(this.check1, this.machineNumberValue, this.pageCurrent, this.pageSize).then(response => {
if (response.data.total === 0) {
this.loading3 = false
} else {
this.total3 = response.data.total
this.tableData3 = response.data.rows
}
}).then(() => {
this.loading3 = false
})
},
searchTable5(row) {
this.loading4 = true
this.tableData4 = []
this.pageCurrent4 = 1
this.machineNumberValue = parseInt(row.机床流水号)
searchtable5(this.machineNumberValue, this.pageCurrent4, this.pageSize4).then(response => {
if (response.data.total === 0) {
this.loading4 = false
} else {
this.total4 = response.data.total
this.tableData4 = response.data.rows
}
}).then(() => {
this.loading4 = false
})
},
searchTable6(row) {
this.loading4 = true
this.tableData4 = []
searchtable5(this.machineNumberValue, this.pageCurrent4, this.pageSize4).then(response => {
if (response.data.total === 0) {
this.loading4 = false
} else {
this.total4 = response.data.total
this.tableData4 = response.data.rows
}
}).then(() => {
this.loading4 = false
})
},
// 分页
handleSizeChange(val) {
this.pageCurrent = 1
@@ -210,6 +454,25 @@ export default {
handleCurrentChange2(val) {
this.pageCurrent2 = val
this.searchTable3()
},
// 分页
handleSizeChange3(val) {
this.pageCurrent3 = 1
this.pageSize3 = val
this.searchTable4()
},
handleCurrentChange3(val) {
this.pageCurrent3 = val
this.searchTable4()
},
handleSizeChange4(val) {
this.pageCurrent4 = 1
this.pageSize4 = val
this.searchTable6()
},
handleCurrentChange4(val) {
this.pageCurrent4 = val
this.searchTable6()
}
}
}