import ExcelJS from 'exceljs'
exportExcel() {
const formattedData = [{
name1:'',
type1:'',
value1:'',
min1:'',
max1:'',
des1:'',
name2:'',
type2:'',
value2:'',
min2:'',
max2:'',
des2:'',
isDifferent:true
}]
const workbook = new ExcelJS.Workbook()
const worksheet = workbook.addWorksheet('Sheet1')
worksheet.mergeCells('A1', 'F1')
worksheet.mergeCells('G1', 'L1')
worksheet.getCell('A1').value = this.oriTableData[0].recipe1Name
worksheet.getCell('G1').value = this.oriTableData[0].recipe2Name
worksheet.getCell('A1').alignment = { horizontal: 'center', vertical: 'middle' }
worksheet.getCell('A1').font = { bold: true }
worksheet.getCell('A1').fill = {
type: 'pattern',
pattern: 'solid',
fgColor: { argb: 'FFFFCC00' }
}
worksheet.getCell('G1').alignment = { horizontal: 'center', vertical: 'middle' }
worksheet.getCell('G1').font = { bold: true }
worksheet.getCell('G1').fill = {
type: 'pattern',
pattern: 'solid',
fgColor: { argb: 'FFFFCC00' }
}
const secondHeader = [
'参数名',
'校验类型',
'参数值',
'最小值',
'最大值',
'描述',
'参数名',
'校验类型',
'参数值',
'最小值',
'最大值',
'描述'
]
const headerRow = worksheet.addRow(secondHeader)
headerRow.eachCell((cell) => {
cell.fill = {
type: 'pattern',
pattern: 'solid',
fgColor: { argb: 'FFCCE5FF' }
}
cell.font = { bold: true }
cell.alignment = { horizontal: 'center', vertical: 'middle' }
})
formattedData.forEach((row) => {
const excelRow = worksheet.addRow([
row.recipe1ParamName,
row.recipe1ValidateType,
row.recipe1ParamValue,
row.recipe1MinValue,
row.recipe1MaxValue,
row.recipe1Remark,
row.recipe2ParamName,
row.recipe2ValidateType,
row.recipe2ParamValue,
row.recipe2MinValue,
row.recipe2MaxValue,
row.recipe2Remark
])
if (row.isDifferent) {
if (row.recipe1ParamValue) {
excelRow.getCell(2).font = { color: { argb: 'FFFF0000' } }
excelRow.getCell(3).font = { color: { argb: 'FFFF0000' } }
excelRow.getCell(4).font = { color: { argb: 'FFFF0000' } }
excelRow.getCell(5).font = { color: { argb: 'FFFF0000' } }
}
if (row.recipe2ParamValue) {
excelRow.getCell(8).font = { color: { argb: 'FFFF0000' } }
excelRow.getCell(9).font = { color: { argb: 'FFFF0000' } }
excelRow.getCell(10).font = { color: { argb: 'FFFF0000' } }
excelRow.getCell(11).font = { color: { argb: 'FFFF0000' } }
}
}
})
worksheet.columns.forEach((column,index) => {
console.log('🚀 ~ worksheet.columns.forEach ~ column,index:', column,index)
let maxLength = 0
column.eachCell({ includeEmpty: false }, (cell) => {
if (cell.value) {
const cellValue = cell.value.toString()
maxLength = Math.max(maxLength, cellValue.length)
}
})
if (index === 0 || index === 6){
column.width = maxLength + 2
} else {
column.width = 15
}
})
workbook.xlsx
.writeBuffer()
.then((buffer) => {
const blob = new Blob([buffer], {
type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
})
const link = document.createElement('a')
link.href = URL.createObjectURL(blob)
link.download = `${this.$moment().format('YYYY-MM-DD')}对比数据.xlsx`
link.click()
URL.revokeObjectURL(link.href)
link.remove()
})
.catch((err) => {
console.error('导出失败:', err)
})
}