Vue項(xiàng)目中如何實(shí)現(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="序號(hào)" width="90" />
<el-table-column prop="nickName" label="昵稱" width="170" />
<el-table-column prop="username" label="賬號(hào)名" width="150" />
</el-table>
</div>
</template>
<script lang="ts" setup>
import * as XLSX from "xlsx";
// 表格數(shù)據(jù)
const tableData = ref([]);
// 選中項(xiàng)
const selectedRows = ref<any[]>([]);
const handleSelectionChange = (val: any[]) => {
selectedRows.value = val;
};
// 導(dǎo)出選中
const handleExportSelected = () => {
if (selectedRows.value.length === 0) {
ElMessage.warning("請(qǐng)先選擇要導(dǎo)出的數(shù)據(jù)");
return;
}
// 準(zhǔn)備導(dǎo)出數(shù)據(jù)
const exportData = selectedRows.value.map((item) => {
return {
用戶ID: item.userId,
客戶昵稱: item.nickName,
賬號(hào)名: 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è)置表頭樣式、實(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("請(qǐng)先選擇要導(dǎo)出的數(shù)據(jù)");
return;
}
// 準(zhǔn)備導(dǎo)出數(shù)據(jù)
const exportData = selectedRows.value.map((item) => {
return {
用戶ID: item.userId,
客戶昵稱: item.nickName,
賬號(hào)名: 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àng)目中如何實(shí)現(xiàn)表格選中數(shù)據(jù)的Excel導(dǎo)出的文章就介紹到這了,更多相關(guān)Vue導(dǎo)出Excel數(shù)據(jù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue完整項(xiàng)目構(gòu)建(進(jìn)階篇)
這篇文章主要介紹了Vue完整項(xiàng)目構(gòu)建(進(jìn)階篇)的相關(guān)資料,需要的朋友可以參考下2018-02-02
Vue 實(shí)現(xiàn)定時(shí)刷新與自動(dòng)更新(示例詳解)
在現(xiàn)代 Web 開發(fā)中,定時(shí)刷新頁(yè)面或定時(shí)更新數(shù)據(jù)是一種常見的需求,尤其是在需要與服務(wù)器進(jìn)行定時(shí)通信或者展示實(shí)時(shí)數(shù)據(jù)的場(chǎng)景下,Vue.js 提供了簡(jiǎn)單而有效的方法來實(shí)現(xiàn)定時(shí)刷新和自動(dòng)更新,本文將介紹幾種常見的定時(shí)刷新方式、適用場(chǎng)景、優(yōu)缺點(diǎn)以及性能考慮2024-11-11
vue+elementUI(el-upload)圖片壓縮,默認(rèn)同比例壓縮操作
這篇文章主要介紹了vue+elementUI(el-upload)圖片壓縮,默認(rèn)同比例壓縮操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-08-08
vue jsx 使用指南及vue.js 使用jsx語(yǔ)法的方法
這篇文章主要介紹了vue jsx 使用指南及vue.js 使用jsx語(yǔ)法的方法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-11-11
詳解Vue-axios 設(shè)置請(qǐng)求頭問題
這篇文章主要介紹了Vue-axios 設(shè)置請(qǐng)求頭問題,文中給大家提到了axios設(shè)置請(qǐng)求頭內(nèi)容的方法,需要的朋友可以參考下2018-12-12
vue實(shí)現(xiàn)文章評(píng)論和回復(fù)列表
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)文章評(píng)論和回復(fù)列表,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04
在vue中v-for循環(huán)遍歷圖片不顯示錯(cuò)誤的解決方案
這篇文章主要介紹了在vue中v-for循環(huán)遍歷圖片不顯示錯(cuò)誤的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01

