Vue使用ExcelJS實(shí)現(xiàn)專(zhuān)業(yè)級(jí)Excel導(dǎo)出解決方案
一、ExcelJS 簡(jiǎn)介:企業(yè)級(jí)Excel處理引擎
ExcelJS 是 Node.js 和瀏覽器環(huán)境中功能最全面的 Excel 處理庫(kù)之一,就像一個(gè)專(zhuān)業(yè)的 Excel 工作站:
[ ExcelJS 核心能力 ]
├─ 高級(jí)樣式控制(字體/顏色/邊框)
├─ 公式計(jì)算與數(shù)據(jù)驗(yàn)證
├─ 圖片/圖表/圖形插入
├─ 多工作表復(fù)雜操作
└─ 流式處理大數(shù)據(jù)集
為什么選擇 ExcelJS?
- 專(zhuān)業(yè)級(jí)功能:支持幾乎所有 Excel 特性
- 樣式控制精細(xì):像素級(jí)樣式調(diào)整能力
- 性能優(yōu)異:流式 API 處理百萬(wàn)行數(shù)據(jù)
- 跨平臺(tái):完美支持 Node.js 和瀏覽器環(huán)境
二、環(huán)境配置與基礎(chǔ)使用
安裝方式
# 通過(guò)npm安裝 npm install exceljs # 瀏覽器直接引入 <script src="https://cdn.jsdelivr.net/npm/exceljs@4.3.0/dist/exceljs.min.js"></script>
最簡(jiǎn)示例詳解
// 1. 創(chuàng)建工作簿 - 好比新建一個(gè)Excel文件
const workbook = new ExcelJS.Workbook();
/*
Workbook 對(duì)象結(jié)構(gòu):
- creator: 創(chuàng)建者信息
- lastModifiedBy: 最后修改者
- created: 創(chuàng)建時(shí)間
- modified: 修改時(shí)間
- worksheets: [] 工作表集合
*/
// 2. 添加工作表 - 添加一個(gè)新的工作表頁(yè)
const worksheet = workbook.addWorksheet('銷(xiāo)售數(shù)據(jù)');
/*
addWorksheet 參數(shù)選項(xiàng):
- name: 工作表名稱(chēng)
- properties: {tabColor, pageSetup等}
- views: 視圖設(shè)置
*/
// 3. 設(shè)置列定義 - 類(lèi)似數(shù)據(jù)庫(kù)表結(jié)構(gòu)
worksheet.columns = [
{ header: '訂單號(hào)', key: 'id', width: 20 },
{ header: '金額', key: 'amount', width: 15, style: { numFmt: '¥#,##0.00' } },
{ header: '日期', key: 'date', width: 15, style: { numFmt: 'yyyy-mm-dd' } }
];
/*
column 配置項(xiàng):
- header: 列標(biāo)題
- key: 數(shù)據(jù)鍵名
- width: 列寬(字符數(shù))
- style: 列默認(rèn)樣式
*/
// 4. 添加數(shù)據(jù)行
worksheet.addRow({ id: 'ORD-20230001', amount: 1999.9, date: new Date() });
/*
addRow 方法特性:
- 接受對(duì)象/數(shù)組格式數(shù)據(jù)
- 自動(dòng)匹配column定義的key
- 返回Row對(duì)象可繼續(xù)操作
*/
// 5. 導(dǎo)出Excel文件
workbook.xlsx.writeBuffer().then(buffer => {
const blob = new Blob([buffer], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' });
saveAs(blob, '銷(xiāo)售報(bào)表.xlsx');
});
/*
writeBuffer 選項(xiàng):
- 返回Promise<Buffer>
- 支持配置壓縮等參數(shù)
*/
三、核心API深度解析
1. 工作表操作
// 獲取工作表
const firstSheet = workbook.getWorksheet(1); // 通過(guò)索引
const targetSheet = workbook.getWorksheet('Sheet1'); // 通過(guò)名稱(chēng)
// 遍歷工作表
workbook.eachSheet((worksheet, sheetId) => {
console.log(`工作表 ${sheetId}: ${worksheet.name}`);
});
// 刪除工作表
workbook.removeWorksheet('Sheet2');
// 工作表屬性設(shè)置
worksheet.properties = {
tabColor: { argb: 'FFFF0000' }, // 標(biāo)簽頁(yè)顏色
defaultRowHeight: 20, // 默認(rèn)行高
defaultColWidth: 10, // 默認(rèn)列寬
pageSetup: { // 頁(yè)面設(shè)置
orientation: 'landscape', // 橫向
margins: { left: 0.7, right: 0.7, top: 0.75, bottom: 0.75 }
}
};
2. 數(shù)據(jù)操作
// 批量添加數(shù)據(jù)
const data = [
{ id: 'ORD-001', amount: 1000 },
{ id: 'ORD-002', amount: 2000 }
];
worksheet.addRows(data);
// 獲取行數(shù)據(jù)
const row = worksheet.getRow(5); // 獲取第5行
console.log(row.getCell(1).value); // 獲取第1列單元格值
// 遍歷數(shù)據(jù)
worksheet.eachRow((row, rowNumber) => {
console.log(`Row ${rowNumber}:`, row.values);
});
// 合并單元格
worksheet.mergeCells('A1:C1'); // 合并A1到C1
worksheet.mergeCells(2, 1, 2, 3); // 行2列1到行2列3
// 數(shù)據(jù)驗(yàn)證
worksheet.getCell('B2').dataValidation = {
type: 'list',
formulae: ['"選項(xiàng)1,選項(xiàng)2,選項(xiàng)3"'],
allowBlank: true
};
3. 樣式設(shè)置
// 創(chuàng)建樣式
const headerStyle = {
font: {
name: '微軟雅黑',
size: 12,
bold: true,
color: { argb: 'FFFFFFFF' } // 白色
},
fill: {
type: 'pattern',
pattern: 'solid',
fgColor: { argb: 'FF4472C4' } // 藍(lán)色背景
},
alignment: {
vertical: 'middle',
horizontal: 'center'
},
border: {
top: { style: 'thin', color: { argb: 'FF000000' } },
left: { style: 'thin', color: { argb: 'FF000000' } },
bottom: { style: 'thin', color: { argb: 'FF000000' } },
right: { style: 'thin', color: { argb: 'FF000000' } }
}
};
// 應(yīng)用樣式
worksheet.getRow(1).eachCell(cell => {
cell.style = headerStyle;
});
// 條件格式
worksheet.eachRow((row, rowNumber) => {
if(rowNumber > 1) { // 跳過(guò)表頭
const amountCell = row.getCell('B');
if(amountCell.value > 1500) {
amountCell.font = { color: { argb: 'FFFF0000' }, bold: true };
}
}
});
四、高級(jí)功能實(shí)現(xiàn)
1. 公式計(jì)算
// 基本公式
worksheet.getCell('D2').value = {
formula: 'B2*C2',
result: 0 // 初始值
};
// 數(shù)組公式
worksheet.getCell('E2').value = {
formula: 'SUM(B2:D2)',
result: 0
};
// 公式計(jì)算(需在Excel中重新計(jì)算)
workbook.calcProperties.fullCalcOnLoad = true;
// 獲取公式值
console.log(worksheet.getCell('D2').result); // 計(jì)算結(jié)果
2. 圖片插入
async function addImageToSheet() {
// 1. 讀取圖片文件
const imageFile = await fetch('logo.png');
const imageBuffer = await imageFile.arrayBuffer();
// 2. 添加圖片到工作表
const imageId = workbook.addImage({
buffer: imageBuffer,
extension: 'png'
});
// 3. 定位圖片位置
worksheet.addImage(imageId, {
tl: { col: 1, row: 1 }, // 左上角位置(列1行1)
br: { col: 3, row: 5 }, // 右下角位置(列3行5)
editAs: 'oneCell' // 定位方式
});
}
3. 圖表生成
// 注意:ExcelJS圖表功能需要Pro版本或配合其他庫(kù)實(shí)現(xiàn)
// 以下是概念性示例:
// 1. 準(zhǔn)備圖表數(shù)據(jù)
worksheet.addRows([
['產(chǎn)品', '銷(xiāo)量'],
['手機(jī)', 1200],
['電腦', 800],
['平板', 600]
]);
// 2. 創(chuàng)建圖表(偽代碼)
const chart = {
type: 'bar', // 柱狀圖
data: {
categories: 'A2:A4', // X軸數(shù)據(jù)
values: 'B2:B4' // Y軸數(shù)據(jù)
},
title: '產(chǎn)品銷(xiāo)量統(tǒng)計(jì)'
};
// 3. 添加圖表到工作表
worksheet.addChart(chart);
五、性能優(yōu)化實(shí)戰(zhàn)
1. 流式寫(xiě)入大數(shù)據(jù)
// Node.js 環(huán)境流式寫(xiě)入
const fs = require('fs');
const { Workbook } = require('exceljs');
async function streamWrite() {
// 1. 創(chuàng)建可寫(xiě)流
const stream = fs.createWriteStream('large-data.xlsx');
// 2. 初始化流式工作簿
const workbook = new Workbook.stream.xlsx.WorkbookWriter({
stream: stream,
useStyles: false, // 禁用樣式提升性能
useSharedStrings: false
});
// 3. 添加工作表
const worksheet = workbook.addWorksheet('大數(shù)據(jù)');
// 4. 模擬大數(shù)據(jù)源
for(let i = 1; i <= 100000; i++) {
worksheet.addRow({
id: `ID-${i}`,
value: Math.random() * 1000,
date: new Date()
}).commit(); // 必須調(diào)用commit
// 每1000行輸出進(jìn)度
if(i % 1000 === 0) {
console.log(`已處理 ${i} 行`);
await new Promise(resolve => setImmediate(resolve));
}
}
// 5. 完成寫(xiě)入
worksheet.commit();
await workbook.commit();
console.log('導(dǎo)出完成');
}
2. 瀏覽器端分塊處理
async function chunkedExport(data, fileName = 'export.xlsx') {
// 1. 初始化工作簿
const workbook = new ExcelJS.Workbook();
const worksheet = workbook.addWorksheet('分塊數(shù)據(jù)');
// 2. 添加表頭
if(data.length > 0) {
worksheet.columns = Object.keys(data[0]).map(key => ({
header: key,
key,
width: 15
}));
}
// 3. 分塊處理
const CHUNK_SIZE = 5000;
let processed = 0;
while(processed < data.length) {
const chunk = data.slice(processed, processed + CHUNK_SIZE);
worksheet.addRows(chunk);
processed += CHUNK_SIZE;
// 更新UI
updateProgress(processed / data.length * 100);
// 釋放主線(xiàn)程
await new Promise(resolve => setTimeout(resolve, 0));
}
// 4. 導(dǎo)出文件
const buffer = await workbook.xlsx.writeBuffer();
saveAs(new Blob([buffer]), fileName);
}
六、企業(yè)級(jí)最佳實(shí)踐
1. 完整的Vue組件實(shí)現(xiàn)
<template>
<div class="excel-export">
<button
@click="handleExport"
:disabled="isExporting"
:class="{ 'exporting': isExporting }"
>
<span v-if="!isExporting">導(dǎo)出Excel</span>
<span v-else>
<span class="spinner"></span>
導(dǎo)出中 ({{ progress }}%)
</span>
</button>
<div v-if="error" class="error-message">
{{ error }}
<button v-if="showRetry" @click="handleExport">重試</button>
</div>
</div>
</template>
<script>
import ExcelJS from 'exceljs';
import { saveAs } from 'file-saver';
export default {
props: {
data: {
type: Array,
required: true,
validator: value => Array.isArray(value) &&
(value.length === 0 || typeof value[0] === 'object')
},
columns: {
type: Array,
default: null
},
fileName: {
type: String,
default: `export_${new Date().toISOString().slice(0, 10)}.xlsx`
},
chunkSize: {
type: Number,
default: 5000
},
styles: {
type: Object,
default: () => ({
header: {
font: { bold: true, color: { argb: 'FFFFFFFF' } },
fill: { type: 'pattern', pattern: 'solid', fgColor: { argb: 'FF4472C4' } }
},
zebra: {
even: { fill: { fgColor: { argb: 'FFF2F2F2' } } },
odd: { fill: { fgColor: { argb: 'FFFFFFFF' } } }
}
})
}
},
data() {
return {
isExporting: false,
progress: 0,
error: null,
showRetry: false
};
},
methods: {
async handleExport() {
this.isExporting = true;
this.error = null;
this.showRetry = false;
try {
// 1. 創(chuàng)建工作簿
const workbook = new ExcelJS.Workbook();
workbook.creator = this.$store.state.user.name || '系統(tǒng)';
workbook.created = new Date();
// 2. 添加工作表
const worksheet = workbook.addWorksheet('導(dǎo)出數(shù)據(jù)');
// 3. 設(shè)置列
if(this.columns) {
worksheet.columns = this.columns;
} else if(this.data.length > 0) {
worksheet.columns = Object.keys(this.data[0]).map(key => ({
header: key,
key,
width: 15
}));
}
// 4. 添加表頭樣式
if(this.styles.header) {
const headerRow = worksheet.getRow(1);
headerRow.eachCell(cell => {
cell.style = this.styles.header;
});
}
// 5. 分塊添加數(shù)據(jù)
for(let i = 0; i < this.data.length; i += this.chunkSize) {
const chunk = this.data.slice(i, i + this.chunkSize);
const addedRows = worksheet.addRows(chunk);
// 應(yīng)用斑馬紋樣式
if(this.styles.zebra) {
addedRows.forEach((row, rowIndex) => {
const isEven = (i + rowIndex) % 2 === 0;
row.eachCell(cell => {
cell.style = {
...cell.style,
...(isEven ? this.styles.zebra.even : this.styles.zebra.odd)
};
});
});
}
// 更新進(jìn)度
this.progress = Math.min(100, (i / this.data.length * 100));
await new Promise(resolve => setTimeout(resolve, 0));
}
// 6. 自動(dòng)調(diào)整列寬
worksheet.columns.forEach(column => {
if(!column.width) {
column.width = column.header.length + 5;
}
});
// 7. 導(dǎo)出文件
const buffer = await workbook.xlsx.writeBuffer();
saveAs(new Blob([buffer]), this.fileName);
// 8. 觸發(fā)完成事件
this.$emit('export-complete', {
fileName: this.fileName,
rowCount: this.data.length
});
} catch (err) {
console.error('導(dǎo)出失敗:', err);
this.error = this.getErrorMessage(err);
this.showRetry = true;
this.$emit('export-error', err);
} finally {
this.isExporting = false;
}
},
getErrorMessage(err) {
if(err.message.includes('memory')) {
return '數(shù)據(jù)量過(guò)大導(dǎo)致內(nèi)存不足,建議分批導(dǎo)出或聯(lián)系管理員';
}
return `導(dǎo)出失敗: ${err.message}`;
}
}
};
</script>
<style scoped>
.excel-export button {
padding: 8px 16px;
background: #4CAF50;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
}
.excel-export button.exporting {
background: #FFC107;
}
.excel-export button:disabled {
background: #cccccc;
cursor: not-allowed;
}
.error-message {
color: #f44336;
margin-top: 8px;
}
.spinner {
display: inline-block;
width: 12px;
height: 12px;
border: 2px solid rgba(255,255,255,0.3);
border-radius: 50%;
border-top-color: white;
animation: spin 1s ease-in-out infinite;
margin-right: 8px;
}
@keyframes spin {
to { transform: rotate(360deg); }
}
</style>
2. 錯(cuò)誤處理策略
| 錯(cuò)誤類(lèi)型 | 檢測(cè)方法 | 處理方案 |
|---|---|---|
| 內(nèi)存不足 | error.message.includes('memory') | 建議分批導(dǎo)出或使用流式API |
| 數(shù)據(jù)格式錯(cuò)誤 | error.message.includes('Invalid') | 驗(yàn)證數(shù)據(jù)格式并提示用戶(hù)修正 |
| 瀏覽器兼容性問(wèn)題 | error.message.includes('support') | 提示用戶(hù)更換瀏覽器 |
| 文件保存被阻止 | error.message.includes('denied') | 檢查瀏覽器下載設(shè)置 |
七、擴(kuò)展應(yīng)用場(chǎng)景
1. 多語(yǔ)言導(dǎo)出
function createMultiLangExport(data, lang = 'zh-CN') {
const workbook = new ExcelJS.Workbook();
const worksheet = workbook.addWorksheet('數(shù)據(jù)');
// 根據(jù)語(yǔ)言選擇表頭
const headers = {
'zh-CN': { id: '訂單號(hào)', amount: '金額', date: '日期' },
'en-US': { id: 'Order ID', amount: 'Amount', date: 'Date' }
};
worksheet.columns = [
{ header: headers[lang].id, key: 'id', width: 20 },
{ header: headers[lang].amount, key: 'amount', width: 15 },
{ header: headers[lang].date, key: 'date', width: 15 }
];
worksheet.addRows(data);
return workbook;
}
// 使用示例
const workbook = createMultiLangExport(data, 'en-US');
workbook.xlsx.writeBuffer().then(/* ... */);
2. 從模板生成報(bào)表
async function generateFromTemplate(templatePath, data) {
// 1. 讀取模板
const templateWorkbook = new ExcelJS.Workbook();
await templateWorkbook.xlsx.readFile(templatePath);
// 2. 獲取模板工作表
const templateSheet = templateWorkbook.getWorksheet('Template');
// 3. 創(chuàng)建新工作簿
const workbook = new ExcelJS.Workbook();
const worksheet = workbook.addWorksheet('Report');
// 4. 復(fù)制模板樣式和結(jié)構(gòu)
worksheet.model = JSON.parse(JSON.stringify(templateSheet.model));
// 5. 填充數(shù)據(jù)
data.forEach((item, index) => {
worksheet.getCell(`A${index + 2}`).value = item.name;
worksheet.getCell(`B${index + 2}`).value = item.value;
});
// 6. 導(dǎo)出
return workbook.xlsx.writeBuffer();
}
八、常見(jiàn)問(wèn)題解答
Q1: 如何設(shè)置單元格為文本格式避免科學(xué)計(jì)數(shù)法?
worksheet.getCell('A1').value = {
text: '00123456', // 文本內(nèi)容
type: ExcelJS.ValueType.String // 明確指定類(lèi)型
};
// 或者批量設(shè)置
worksheet.getColumn('A').eachCell(cell => {
cell.numFmt = '@'; // @表示文本格式
});
Q2: 如何導(dǎo)出帶超鏈接的單元格?
worksheet.getCell('A1').value = {
text: '公司官網(wǎng)',
hyperlink: 'https://example.com',
tooltip: '點(diǎn)擊訪(fǎng)問(wèn)官網(wǎng)'
};
// 或者使用純文本方式
worksheet.getCell('A2').value = '=HYPERLINK("https://example.com", "官網(wǎng)")';
Q3: 如何實(shí)現(xiàn)凍結(jié)窗格?
worksheet.views = [
{
state: 'frozen',
xSplit: 1, // 凍結(jié)第1列右側(cè)
ySplit: 1, // 凍結(jié)第1行下方
topLeftCell: 'B2', // 活動(dòng)單元格
activeCell: 'B2'
}
];
結(jié)語(yǔ):ExcelJS 專(zhuān)業(yè)級(jí)導(dǎo)出方案
通過(guò)本指南,您已經(jīng)掌握:
- ExcelJS 的核心功能和架構(gòu)設(shè)計(jì)
- 從基礎(chǔ)到高級(jí)的各種數(shù)據(jù)操作技巧
- 專(zhuān)業(yè)樣式和高級(jí)功能實(shí)現(xiàn)方法
- 大數(shù)據(jù)量下的性能優(yōu)化策略
- 企業(yè)級(jí)項(xiàng)目的最佳實(shí)踐方案
專(zhuān)業(yè)建議:
- 對(duì)于簡(jiǎn)單需求,xlsx 可能更輕便
- 需要精細(xì)樣式控制時(shí)首選 ExcelJS
- 超過(guò)50萬(wàn)行數(shù)據(jù)考慮流式寫(xiě)入
- 復(fù)雜業(yè)務(wù)邏輯推薦使用模板方式
ExcelJS 就像專(zhuān)業(yè)的 Excel 工作站,為您提供企業(yè)級(jí)的數(shù)據(jù)導(dǎo)出能力?,F(xiàn)在,您可以自信地應(yīng)對(duì)任何復(fù)雜的 Excel 導(dǎo)出需求了!
到此這篇關(guān)于Vue使用ExcelJS實(shí)現(xiàn)專(zhuān)業(yè)級(jí)Excel導(dǎo)出解決方案的文章就介紹到這了,更多相關(guān)ExcelJS導(dǎo)出Excel內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue2之簡(jiǎn)易的pc端短信驗(yàn)證碼的問(wèn)題及處理方法
這篇文章主要介紹了vue2之簡(jiǎn)易的pc端短信驗(yàn)證碼的問(wèn)題,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-06-06
如何把vuejs打包出來(lái)的文件整合到springboot里
這篇文章主要介紹了如何把vuejs打包出來(lái)的文件整合到springboot里,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-07-07
在vue中使用inheritAttrs實(shí)現(xiàn)組件的擴(kuò)展性介紹
這篇文章主要介紹了在vue中使用inheritAttrs實(shí)現(xiàn)組件的擴(kuò)展性介紹,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-12-12
詳解Vue3中shallowRef和shallowReactive的使用
這篇文章主要為大家介紹了Vue3中shallowRef和shallowReactive函數(shù)的使用方法,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)Vue有一定的幫助,需要的可以參考一下2022-07-07
詳解如何解決Vue和vue-template-compiler版本之間的問(wèn)題
這篇文章主要介紹了詳解如何解決Vue和vue-template-compiler版本之間的問(wèn)題,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-09-09
Vue實(shí)現(xiàn)漸變色進(jìn)度條的代碼
這篇文章主要介紹了Vue實(shí)現(xiàn)漸變色進(jìn)度條的代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04
vue項(xiàng)目,代碼提交至碼云,iconfont的用法說(shuō)明
這篇文章主要介紹了vue項(xiàng)目,代碼提交至碼云,iconfont的用法說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-07-07

