原生方法(简单)
https://blog.csdn.net/qq_41579327/article/details/125088468?spm=1011.2415.3001.5331
纯前端方法
vue、vite、webpack环境多可用
安装
bash
npm i exceljs
使用
javascript
import ExcelJS from 'exceljs'
/**
* data: [{tital1:'', tital2:'', tital3:'', tital4:'', tital5:'', tital6:'', tital7:'', tital8:'', tital9:'', tital10:'', tital11:''}...]
**/
const exportExcel = async (data) => {
const workbook = new ExcelJS.Workbook()
const worksheet = workbook.addWorksheet('sheet1', {
properties: { defaultRowHeight: 36, defaultColWidth: 20 },
// 打印配置
pageSetup: {
horizontalCentered: true,
verticalCentered: true,
paperSize: 9,
orientation: 'landscape',
scale: 81,
margins: {
left: 0.25,
right: 0.25,
top: 0.75,
bottom: 0.75,
header: 0.3,
footer: 0.3
}
}
})
worksheet.mergeCells('A1:K1') // 合并单元格
const a1 = worksheet.getCell('A1') // 获取A1单元格
a1.value = '抽查表' // 设置内容
a1.alignment = { vertical: 'top', horizontal: 'center' } // 设置内容居中
a1.font = { size: 20, bold: true } // 设置字体
worksheet.getRow(1).height = 40 // 设置行高
const a2 = worksheet.getCell('A2')
a2.value = '抽查日期:'
a2.alignment = { vertical: 'middle', horizontal: 'left' }
a2.font = { size: 14, bold: true }
worksheet.mergeCells('J2:K2')
const j2 = worksheet.getCell('J2')
j2.value = `统计时间:${new Date().toISOString().slice(0, 10)}`
j2.alignment = { vertical: 'middle', horizontal: 'right' }
j2.font = { size: 14 }
worksheet.getRow(2).height = 40
const header_title = ['tital1', 'tital2', 'tital3', 'tital4', 'tital5', 'tital6', 'tital7', 'tital8', 'tital9', 'tital10', 'tital11']
worksheet.addRow(header_title)
if (data && Array.isArray(data)) {
data.forEach((item, index) => {
worksheet.addRow([index + 1, item.code, item.name, item.type, item.unit, item.cangkuName, item.huoweiName, item.kucun, '', '', ''])
})
}
const columnWidths = [7, 13, 19, 31, 12, 14, 19, 14, 11, 15, 23]
columnWidths.forEach((width, index) => {
worksheet.getColumn(index + 1).width = width
})
const alignment = {
wrapText: true,
vertical: 'middle',
horizontal: 'center'
}
const borderStyle = {
style: 'thin',
color: { argb: 'ff000000' }
}
const border = {
top: borderStyle,
left: borderStyle,
bottom: borderStyle,
right: borderStyle
}
const data_rows = data?.length ? data.length + 3 : 13
const data_cols = header_title.length || 11
for (let row = 3; row <= data_rows; row++) {
worksheet.getRow(row).height = 36
for (let col = 1; col <= data_cols; col++) {
const cell = worksheet.getCell(row, col)
cell.font = { size: 12, bold: row === 3 }
cell.alignment = alignment
cell.border = border // 设置边框
}
}
const footer_rows = data_rows + 1
worksheet.getRow(footer_rows).height = 30
worksheet.mergeCells(`A${footer_rows}:B${footer_rows}`)
const a14 = worksheet.getCell(`A${footer_rows}`)
a14.value = '抽查人:'
a14.font = { size: 14 }
const e14 = worksheet.getCell(`E${footer_rows}`)
e14.value = '核对人:'
e14.font = { size: 14 }
const k14 = worksheet.getCell(`K${footer_rows}`)
k14.value = '准确率:'
k14.font = { size: 14 }
// 生成文件并下载
const buffer = await workbook.xlsx.writeBuffer()
saveAs(new Blob([buffer]), `导出数据_${new Date().toLocaleDateString()}.xlsx`)
}
后端nodeJS方法
使用node处理数据,再导出excel表格
安装
bash
npm i exceljs
使用
javascript
const ExcelJS = require('exceljs')
const fs = require('fs')
const exportExcel = async (data, filePath) => {
const workbook = new ExcelJS.Workbook()
const worksheet = workbook.addWorksheet('数据列表', {
properties: { defaultRowHeight: 36, defaultColWidth: 20 },
pageSetup: {
horizontalCentered: true,
verticalCentered: true,
paperSize: 9,
orientation: 'landscape',
scale: 81,
margins: {
left: 0.25,
right: 0.25,
top: 0.75,
bottom: 0.75,
header: 0.3,
footer: 0.3
}
}
})
// 按换行拆分
let lines = data.split('\n').filter((line) => line.trim() !== '')
const rows = lines.map((line) => line.split(' ').filter((item) => item.trim() !== ''))
rows.forEach((row) => {
worksheet.addRow(row)
})
// 设置所有单元格居中
worksheet.eachRow((row) => {
row.eachCell((cell) => {
cell.alignment = { vertical: 'middle', horizontal: 'center' }
})
})
const buffer = await workbook.xlsx.writeBuffer()
// 保存文件
fs.writeFileSync(filePath, buffer)
console.log(`Excel文件已保存到 ${filePath}`)
}
const str = `
故障 显示 名称 类型 复位 范围 错码 辅码
E2 E2.0 外设异常 NO.1 否 整机 x0E12 x0E120
E3 E3.0 设定错误 NO.2 是 整机 x6320 x0E130
E5 E5.0 误差过大 NO.2 是 整机 x0E15 x0E150
`
exportExcel(str, './output.xlsx')