Vue導(dǎo)出Excel文件的四種實(shí)現(xiàn)方式
Vue.js 本身不提供直接導(dǎo)出 Excel 的功能,但可以通過以下幾種方式實(shí)現(xiàn):
1.前端導(dǎo)出方案
使用 xlsx 庫(推薦)
npm install xlsx # 或 yarn add xlsx
<template>
<button @click="exportExcel">導(dǎo)出Excel</button>
</template>
<script>
import * as XLSX from 'xlsx';
export default {
data() {
return {
tableData: [
{ name: '張三', age: 25, city: '北京' },
{ name: '李四', age: 30, city: '上海' }
]
}
},
methods: {
exportExcel() {
// 創(chuàng)建工作簿
const wb = XLSX.utils.book_new();
// 創(chuàng)建工作表
const ws = XLSX.utils.json_to_sheet(this.tableData);
// 將工作表添加到工作簿
XLSX.utils.book_append_sheet(wb, ws, 'Sheet1');
// 導(dǎo)出文件
XLSX.writeFile(wb, '用戶數(shù)據(jù).xlsx');
}
}
}
</script>使用 exceljs(功能更強(qiáng)大)
npm install exceljs npm install file-saver
import ExcelJS from 'exceljs';
import { saveAs } from 'file-saver';
async exportExcel() {
const workbook = new ExcelJS.Workbook();
const worksheet = workbook.addWorksheet('Sheet1');
// 添加表頭
worksheet.columns = [
{ header: '姓名', key: 'name' },
{ header: '年齡', key: 'age' },
{ header: '城市', key: 'city' }
];
// 添加數(shù)據(jù)
this.tableData.forEach(item => {
worksheet.addRow(item);
});
// 保存文件
const buffer = await workbook.xlsx.writeBuffer();
const blob = new Blob([buffer], {
type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
});
saveAs(blob, '用戶數(shù)據(jù).xlsx');
}2.使用現(xiàn)成組件
vue-json-excel
npm install vue-json-excel
<template>
<download-excel
:data="tableData"
:fields="jsonFields"
name="用戶數(shù)據(jù).xlsx"
>
<button>導(dǎo)出Excel</button>
</download-excel>
</template>
<script>
import JsonExcel from 'vue-json-excel';
export default {
components: {
'download-excel': JsonExcel
},
data() {
return {
tableData: [...],
jsonFields: {
'姓名': 'name',
'年齡': 'age',
'城市': 'city'
}
}
}
}
</script>3.后端生成方案
如果數(shù)據(jù)量大或需要復(fù)雜格式,建議后端生成:
// 前端調(diào)用
exportExcel() {
axios({
url: '/api/export/excel',
method: 'GET',
responseType: 'blob' // 重要:接收文件流
}).then(response => {
const blob = new Blob([response.data], {
type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
});
const url = window.URL.createObjectURL(blob);
const link = document.createElement('a');
link.href = url;
link.download = 'data.xlsx';
link.click();
window.URL.revokeObjectURL(url);
});
}4.簡單表格導(dǎo)出
exportTable() {
const table = document.querySelector('#your-table');
const wb = XLSX.utils.table_to_book(table);
XLSX.writeFile(wb, '表格數(shù)據(jù).xlsx');
}注意事項(xiàng)
- 數(shù)據(jù)量大時(shí):建議使用后端導(dǎo)出,避免瀏覽器內(nèi)存溢出
- 格式化處理:日期、數(shù)字等特殊格式需要轉(zhuǎn)換
- 樣式需求:如需復(fù)雜樣式,建議使用 exceljs
- 兼容性:xlsx 庫兼容性較好,支持多種格式
推薦方案:
- 簡單場景:使用 xlsx 庫
- 需要樣式和復(fù)雜功能:使用 exceljs
- 大數(shù)據(jù)量:后端生成
到此這篇關(guān)于Vue導(dǎo)出Excel文件的四種實(shí)現(xiàn)方式的文章就介紹到這了,更多相關(guān)Vue導(dǎo)出Excel文件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue Element前端應(yīng)用開發(fā)之獲取后端數(shù)據(jù)
這篇文章主要介紹了Vue Element前端應(yīng)用開發(fā)之獲取后端數(shù)據(jù),對vue感興趣的同學(xué),可以參考下2021-05-05
在UniApp中實(shí)現(xiàn)App與H5頁面的跳轉(zhuǎn)及通信的代碼示例
在移動(dòng)應(yīng)用開發(fā)中,內(nèi)嵌 H5 頁面或與外部網(wǎng)頁交互是常見需求,UniApp 作為跨平臺(tái)框架,提供了靈活的方式實(shí)現(xiàn) App 與 H5 的跳轉(zhuǎn)和雙向通信,本文將詳細(xì)講解實(shí)現(xiàn)方法,并提供可直接復(fù)用的代碼示例,需要的朋友可以參考下2025-04-04
利用vue3+threejs仿iView官網(wǎng)大波浪特效實(shí)例
最近好幾個(gè)vue項(xiàng)目都是用ivew作為UI框架,所以下面這篇文章主要給大家介紹了關(guān)于如何利用vue3?+?threejs仿iView官網(wǎng)大波浪特效的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2021-12-12

