element-easy
Version:
crud for element ui
48 lines (45 loc) • 1.4 kB
JavaScript
import ExcelJS from 'exceljs/dist/es5/exceljs.browser'
import { saveAs } from 'file-saver'
export function exportToExcel ({ thead, tbody }) {
const workbook = new ExcelJS.Workbook()
const worksheet = workbook.addWorksheet('My Sheet')
worksheet.columns = thead
.filter((item) => item.prop)
.map((item) => {
return {
header: item.label,
key: item.prop,
width: item.width ? item.width / 10 : 30,
style: {
alignment: { vertical: 'middle', horizontal: item.align }
}
}
})
worksheet.addRows(
tbody.map((item) => {
const res = []
thead.forEach((column) => {
if (item[column.prop] !== undefined) res.push(item[column.prop])
})
return res
})
)
// worksheet._rows.forEach((row) => {
// row._cells.forEach((cell) => {
// cell.border = {
// top: { style: 'thin', color: { argb: '00000000' } },
// left: { style: 'thin', color: { argb: '00000000' } },
// bottom: { style: 'thin', color: { argb: '00000000' } },
// right: { style: 'thin', color: { argb: '00000000' } }
// }
// })
// })
workbook.xlsx.writeBuffer().then((uint8) => {
saveAs(
new Blob([uint8.buffer], {
type: 'application/octet-stream'
}),
new Date().getTime() + '.xlsx'
)
})
}