Vue項目中如何實現(xiàn)表格選中數(shù)據(jù)的Excel導(dǎo)出
一、安裝
npm install xlsx
二、核心代碼
<template>
<div class="statistics-container">
<el-button type="primary" @click="handleExportSelected">導(dǎo)出選中</el-button>
<el-table
:data="tableData"
border
style="width: 100%"
@selection-change="handleSelectionChange"
>
<el-table-column type="selection" width="55" />
<el-table-column prop="index" label="序號" width="90" />
<el-table-column prop="nickName" label="昵稱" width="170" />
<el-table-column prop="username" label="賬號名" width="150" />
</el-table>
</div>
</template>
<script lang="ts" setup>
import * as XLSX from "xlsx";
// 表格數(shù)據(jù)
const tableData = ref([]);
// 選中項
const selectedRows = ref<any[]>([]);
const handleSelectionChange = (val: any[]) => {
selectedRows.value = val;
};
// 導(dǎo)出選中
const handleExportSelected = () => {
if (selectedRows.value.length === 0) {
ElMessage.warning("請先選擇要導(dǎo)出的數(shù)據(jù)");
return;
}
// 準(zhǔn)備導(dǎo)出數(shù)據(jù)
const exportData = selectedRows.value.map((item) => {
return {
用戶ID: item.userId,
客戶昵稱: item.nickName,
賬號名: item.username,
};
});
// 創(chuàng)建工作簿
const ws = XLSX.utils.json_to_sheet(exportData);
const wb = XLSX.utils.book_new();
XLSX.utils.book_append_sheet(wb, ws, "列表數(shù)據(jù)");
// 導(dǎo)出文件
const fileName = `列表數(shù)據(jù)_選中_${new Date().getTime()}.xlsx`;
XLSX.writeFile(wb, fileName);
ElMessage.success(`成功導(dǎo)出${selectedRows.value.length}條數(shù)據(jù)`);
};
</script>
三、設(shè)置表頭樣式、實現(xiàn)列寬自適應(yīng)
安裝xlsx-js-style,設(shè)置單元格樣式
npm install xlsx-js-style
import XLSX from "xlsx-js-style";
// 導(dǎo)出選中
const handleExportSelected = () => {
if (selectedRows.value.length === 0) {
ElMessage.warning("請先選擇要導(dǎo)出的數(shù)據(jù)");
return;
}
// 準(zhǔn)備導(dǎo)出數(shù)據(jù)
const exportData = selectedRows.value.map((item) => {
return {
用戶ID: item.userId,
客戶昵稱: item.nickName,
賬號名: item.username,
};
});
// 創(chuàng)建工作簿
const ws = XLSX.utils.json_to_sheet(exportData);
// 設(shè)置表頭樣式
const headerStyle = {
font: {
name: "Arial",
sz: 12,
bold: true,
color: { rgb: "000000" },
},
fill: {
fgColor: { rgb: "f3fff1" },
},
alignment: {
horizontal: "center",
vertical: "center",
},
border: {
top: { style: "thin", color: { rgb: "000000" } },
bottom: { style: "thin", color: { rgb: "000000" } },
left: { style: "thin", color: { rgb: "000000" } },
right: { style: "thin", color: { rgb: "000000" } },
},
};
// 應(yīng)用表頭樣式
const range = XLSX.utils.decode_range(ws["!ref"]);
for (let col = range.s.c; col <= range.e.c; col++) {
const cell = ws[XLSX.utils.encode_cell({ r: 0, c: col })];
if (cell) {
cell.s = headerStyle;
}
}
// 設(shè)置列寬自適應(yīng)
const colWidths = [];
for (let col = range.s.c; col <= range.e.c; col++) {
let maxWidth = 10;
for (let row = range.s.r; row <= range.e.r; row++) {
const cell = ws[XLSX.utils.encode_cell({ r: row, c: col })];
if (cell && cell.v) {
const cellLength = cell.v.toString().length;
if (cellLength > maxWidth) {
maxWidth = cellLength;
}
}
}
// 限制最大寬度為50,最小寬度為20
colWidths.push({ wch: Math.min(Math.max(maxWidth + 2, 20), 50) });
}
ws["!cols"] = colWidths;
const wb = XLSX.utils.book_new();
console.log(ws);
XLSX.utils.book_append_sheet(wb, ws, "列表數(shù)據(jù)");
// 導(dǎo)出文件
const fileName = `列表數(shù)據(jù)_選中_${new Date().getTime()}.xlsx`;
XLSX.writeFile(wb, fileName);
ElMessage.success(`成功導(dǎo)出${selectedRows.value.length}條數(shù)據(jù)`);
};
四、效果展示

到此這篇關(guān)于Vue項目中如何實現(xiàn)表格選中數(shù)據(jù)的Excel導(dǎo)出的文章就介紹到這了,更多相關(guān)Vue導(dǎo)出Excel數(shù)據(jù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue3訪問頁面時自動獲取數(shù)據(jù)的方法實現(xiàn)
本文介紹了在Vue3中如何利用生命周期鉤子函數(shù)和定時器實現(xiàn)訪問頁面時自動獲取數(shù)據(jù)的方法,這種方法適用于需要在頁面加載時即時更新數(shù)據(jù)顯示的場景,感興趣的可以了解一下2024-11-11
Vue3+TypeScript項目中安裝PDF.js詳細(xì)的步驟
這篇文章主要介紹了Vue3+TypeScript項目中安裝PDF.js詳細(xì)的步驟,通過示例代碼詳細(xì)介紹了包括安裝步驟、基礎(chǔ)使用示例、創(chuàng)建可復(fù)用PDF查看器組件、注意事項等,需要的朋友可以參考下2026-01-01
解決Vue3.0刷新頁面警告[Vue Router warn]:No match 
這篇文章主要介紹了解決Vue3.0刷新頁面警告[Vue Router warn]:No match found for location with path /xxx問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-03-03
vue.js過濾器+ajax實現(xiàn)事件監(jiān)聽及后臺php數(shù)據(jù)交互實例
這篇文章主要介紹了vue.js過濾器+ajax實現(xiàn)事件監(jiān)聽及后臺php數(shù)據(jù)交互,結(jié)合實例形式分析了vue.js前臺過濾器與ajax后臺數(shù)據(jù)交互相關(guān)操作技巧,需要的朋友可以參考下2018-05-05

