1.使用XLSX前先安装,在vue文件引用
<div class="dialog-footer" style="text-align: center;padding: 4px 0;">
<el-button @click="excelData" type="primary">导出</el-button>
</div>
import * as XLSX from 'xlsx'
methods: {
// 导出excel
excelData() {
try {
const nowTime = formatDate(new Date(), 'yyyy-MM-dd hh:mm:ss')
const filename = `智能测试数据${nowTime}.xlsx`
const workbook = XLSX.utils.book_new(); // 创建工作簿
for (let i = 0; i < 4; i++) {
const data = {
sheetName: 'CH' + (i + 1),// 分页名称
// 导出excel的表头
tHeader: ['序号', '工步名称', '电流(A)', '电压(V)', '协议', '模式', '测试电流(A)', '测试电压(V)', '测试范围(V)', '测试结果(V)', '测试时间'],
// filterVal 与中文表头对应对象属性名称
filterVal: ['num', 'name', 'writeElectricity', 'writeVoltage', 'protocol', 'mode', 'electricity', 'voltage', 'scope', 'results', 'testDate'],
list: this.testData[i].printData
};
const formattedData = this.formatJson(data.filterVal, data.list);
const worksheetData = [data.tHeader, ...formattedData]; // 添加标题行和数据行
const worksheet = XLSX.utils.aoa_to_sheet(worksheetData); // 创建工作表
XLSX.utils.book_append_sheet(workbook, worksheet, data.sheetName) // 将工作表添加到工作簿
}
XLSX.writeFile(workbook, filename); // 导出Excel文件
} catch (error) {
console.log(error)
}
},
formatJson(filterVal, jsonData) {
return jsonData.map(v => filterVal.map(j => v[j]))
}
}
2.导出数据为
chList: [
{
label: "CH1",
name: "first",
tableDatas: [
{
name: "PD5V1A",
protocol: 3,
currentPro: 3,
tabPosition: "CC",
voltage: "5.0",
decoyVoltage: "5.0",
electricity: "1.0",
upLimit: "5.5",
lowerLimit: "4.5",
isStart: false,
testval: "0",
scope: "4.5~5.5",
time: "0",
timeout: "0"
},
{
name: "PD9V1A",
protocol: 3,
currentPro: 3,
tabPosition: "CC",
voltage: "9.0",
decoyVoltage: "9.0",
electricity: "1.0",
upLimit: "9.5",
lowerLimit: "8.5",
isStart: false,
testval: "0",
scope: "8.5~9.5",
time: "0",
timeout: "0"
},
{
name: "PD12V1A",
protocol: 3,
currentPro: 3,
tabPosition: "CC",
voltage: "12.0",
decoyVoltage: "12.0",
electricity: "1.0",
upLimit: "12.5",
lowerLimit: "11.5",
isStart: false,
testval: "0",
scope: "11.5~12.5",
time: "0",
timeout: "0"
},
{
name: "PD15V1A",
protocol: 3,
currentPro: 3,
tabPosition: "CC",
voltage: "15.0",
decoyVoltage: "15.0",
electricity: "1.0",
upLimit: "15.5",
lowerLimit: "14.5",
isStart: false,
testval: "0",
scope: "14.5~15.5",
time: "0",
timeout: "0"
},
{
name: "PD20V1A",
protocol: 3,
currentPro: 3,
tabPosition: "CC",
voltage: "20.0",
decoyVoltage: "20.0",
electricity: "1.0",
upLimit: "20.5",
lowerLimit: "19.5",
isStart: false,
testval: "0",
scope: "19.5~20.5",
time: "0",
timeout: "0"
}
]
},
{
label: "CH2",
name: "second",
tableDatas: [
{
name: "PD5V1A",
protocol: 3,
currentPro: 3,
tabPosition: "CC",
voltage: "5.0",
decoyVoltage: "5.0",
electricity: "1.0",
upLimit: "5.5",
lowerLimit: "4.5",
isStart: false,
testval: "0",
scope: "4.5~5.5",
time: "0",
timeout: "0"
},
{
name: "PD9V1A",
protocol: 3,
currentPro: 3,
tabPosition: "CC",
voltage: "9.0",
decoyVoltage: "9.0",
electricity: "1.0",
upLimit: "9.5",
lowerLimit: "8.5",
isStart: false,
testval: "0",
scope: "8.5~9.5",
time: "0",
timeout: "0"
},
{
name: "PD12V1A",
protocol: 3,
currentPro: 3,
tabPosition: "CC",
voltage: "12.0",
decoyVoltage: "12.0",
electricity: "1.0",
upLimit: "12.5",
lowerLimit: "11.5",
isStart: false,
testval: "0",
scope: "11.5~12.5",
time: "0",
timeout: "0"
},
{
name: "PD15V1A",
protocol: 3,
currentPro: 3,
tabPosition: "CC",
voltage: "15.0",
decoyVoltage: "15.0",
electricity: "1.0",
upLimit: "15.5",
lowerLimit: "14.5",
isStart: false,
testval: "0",
scope: "14.5~15.5",
time: "0",
timeout: "0"
},
{
name: "PD20V1A",
protocol: 3,
currentPro: 3,
tabPosition: "CC",
voltage: "20.0",
decoyVoltage: "20.0",
electricity: "1.0",
upLimit: "20.5",
lowerLimit: "19.5",
isStart: false,
testval: "0",
scope: "19.5~20.5",
time: "0",
timeout: "0"
}
]
},
{
label: "CH3",
name: "third",
tableDatas: [
{
name: "PD5V1A",
protocol: 3,
currentPro: 3,
tabPosition: "CC",
voltage: "5.0",
decoyVoltage: "5.0",
electricity: "1.0",
upLimit: "5.5",
lowerLimit: "4.5",
isStart: false,
testval: "0",
scope: "4.5~5.5",
time: "0",
timeout: "0"
},
{
name: "PD9V1A",
protocol: 3,
currentPro: 3,
tabPosition: "CC",
voltage: "9.0",
decoyVoltage: "9.0",
electricity: "1.0",
upLimit: "9.5",
lowerLimit: "8.5",
isStart: false,
testval: "0",
scope: "8.5~9.5",
time: "0",
timeout: "0"
},
{
name: "PD12V1A",
protocol: 3,
currentPro: 3,
tabPosition: "CC",
voltage: "12.0",
decoyVoltage: "12.0",
electricity: "1.0",
upLimit: "12.5",
lowerLimit: "11.5",
isStart: false,
testval: "0",
scope: "11.5~12.5",
time: "0",
timeout: "0"
},
{
name: "PD15V1A",
protocol: 3,
currentPro: 3,
tabPosition: "CC",
voltage: "15.0",
decoyVoltage: "15.0",
electricity: "1.0",
upLimit: "15.5",
lowerLimit: "14.5",
isStart: false,
testval: "0",
scope: "14.5~15.5",
time: "0",
timeout: "0"
},
{
name: "PD20V1A",
protocol: 3,
currentPro: 3,
tabPosition: "CC",
voltage: "20.0",
decoyVoltage: "20.0",
electricity: "1.0",
upLimit: "20.5",
lowerLimit: "19.5",
isStart: false,
testval: "0",
scope: "19.5~20.5",
time: "0",
timeout: "0"
}
]
},
{
label: "CH4",
name: "fourth",
tableDatas: [
{
name: "PD5V1A",
protocol: 3,
currentPro: 3,
tabPosition: "CC",
voltage: "5.0",
decoyVoltage: "5.0",
electricity: "1.0",
upLimit: "5.5",
lowerLimit: "4.5",
isStart: false,
testval: "0",
scope: "4.5~5.5",
time: "0",
timeout: "0"
},
{
name: "PD9V1A",
protocol: 3,
currentPro: 3,
tabPosition: "CC",
voltage: "9.0",
decoyVoltage: "9.0",
electricity: "1.0",
upLimit: "9.5",
lowerLimit: "8.5",
isStart: false,
testval: "0",
scope: "8.5~9.5",
time: "0",
timeout: "0"
},
{
name: "PD12V1A",
protocol: 3,
currentPro: 3,
tabPosition: "CC",
voltage: "12.0",
decoyVoltage: "12.0",
electricity: "1.0",
upLimit: "12.5",
lowerLimit: "11.5",
isStart: false,
testval: "0",
scope: "11.5~12.5",
time: "0",
timeout: "0"
},
{
name: "PD15V1A",
protocol: 3,
currentPro: 3,
tabPosition: "CC",
voltage: "15.0",
decoyVoltage: "15.0",
electricity: "1.0",
upLimit: "15.5",
lowerLimit: "14.5",
isStart: false,
testval: "0",
scope: "14.5~15.5",
time: "0",
timeout: "0"
},
{
name: "PD20V1A",
protocol: 3,
currentPro: 3,
tabPosition: "CC",
voltage: "20.0",
decoyVoltage: "20.0",
electricity: "1.0",
upLimit: "20.5",
lowerLimit: "19.5",
isStart: false,
testval: "0",
scope: "19.5~20.5",
time: "0",
timeout: "0"
}
]
}
],
3.导出后的表格为