67 lines
2.3 KiB
Plaintext
67 lines
2.3 KiB
Plaintext
import Excel from 'exceljs'
|
|
export function exportExcelWithTotal(sheetName, columns, title, conditional, headerArr, data, totalRow, numFmts, merge) {
|
|
var workbook = new Excel.Workbook()
|
|
var sheet = workbook.addWorksheet(sheetName)
|
|
sheet.columns = columns
|
|
const maxColCode = String.fromCharCode(64 + columns.length)
|
|
// 处理标题
|
|
sheet.mergeCells(`A1:${maxColCode}2`)
|
|
sheet.getCell('A1').value = title
|
|
sheet.getCell('A1').alignment = { vertical: 'middle', horizontal: 'center' }
|
|
sheet.getCell('A1').font = { name: 'SimSun', family: 4, size: 13, bold: true }
|
|
sheet.mergeCells(`A3:${maxColCode}3`)
|
|
sheet.getCell('A3').value = conditional
|
|
sheet.getCell('A3').alignment = { vertical: 'middle', horizontal: 'right' }
|
|
// 列头
|
|
sheet.getRow(4).values = headerArr
|
|
sheet.getRow(4).font = { name: 'SimSun', family: 4, size: 11, bold: true }
|
|
sheet.getRow(4).alignment = { vertical: 'middle', horizontal: 'left' }
|
|
|
|
sheet.addRows(data)
|
|
numFmts.forEach(element => {
|
|
sheet.getColumn(element.colIndex).numFmt = element.format
|
|
})
|
|
sheet.addRow(totalRow)
|
|
sheet.getRow(data.length + 5).font = { name: 'SimSun', family: 4, size: 11, bold: true }
|
|
if (merge) {
|
|
sheet.mergeCells(merge)
|
|
}
|
|
sheet.eachRow((row, number) => {
|
|
if (number > 3) {
|
|
row.eachCell(cell => {
|
|
cell.alignment = { vertical: 'center', horizontal: 'left' }
|
|
cell.border = {
|
|
top: { style: 'thin' },
|
|
left: { style: 'thin' },
|
|
bottom: { style: 'thin' },
|
|
right: { style: 'thin' }
|
|
}
|
|
})
|
|
}
|
|
})
|
|
|
|
workbook.xlsx
|
|
.writeBuffer({
|
|
base64: true
|
|
})
|
|
.then(function(xls64) {
|
|
var data = new Blob([xls64], {
|
|
type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
|
|
})
|
|
if ('msSaveOrOpenBlob' in navigator) {
|
|
window.navigator.msSaveOrOpenBlob(data, `${sheetName} ${title}.xlsx`)
|
|
} else {
|
|
var a = document.createElement('a')
|
|
var url = URL.createObjectURL(data)
|
|
a.href = url
|
|
a.download = `${sheetName} ${title}.xlsx`
|
|
document.body.appendChild(a)
|
|
a.click()
|
|
setTimeout(function() {
|
|
document.body.removeChild(a)
|
|
window.URL.revokeObjectURL(url)
|
|
}, 0)
|
|
}
|
|
})
|
|
}
|