vue3在线预览excel表格

在 Vue 3 项目中实现 Excel 表格在线预览。

**使用 @vue-office/excel(支持 .xlsx/.xls)**‌

该库专为 Vue 3 设计,支持 Word、Excel、PPT、PDF 等多种格式,集成简单、样式保留较好。

安装依赖

npm install @vue-office/excel @vue-office/docx @vue-office/pptx @vue-office/pdf

引入样式(重要)

import '@vue-office/excel/lib/index.css'

使用组件

<template>
<div class="file-preview-container">
<VueOfficeExcel
v-if="'xlsx', 'xls'.includes(fileType)"
:src="fileSrc"
:style="{ width: '100%', height: '600px', border: '1px solid #e8e8e8' }"
@rendered="handleRendered"
@error="handleError"
/>
</div>
</template>

<script setup>
import { ref, computed } from 'vue'
import VueOfficeExcel from '@vue-office/excel'

const props = defineProps({
src: String // 可为 URL、Blob、File 或 ArrayBuffer
})

const fileSrc = ref(props.src)
const fileType = computed(() => {
if (!props.src) return ''
const filename = typeof props.src === 'string' ? props.src.toLowerCase() : props.src.name?.toLowerCase()
if (filename.endsWith('.xlsx')) return 'xlsx'
if (filename.endsWith('.xls')) return 'xls'
return ''
})

const handleRendered = () => console.log('Excel 渲染完成')
const handleError = (err) => console.error('渲染失败:', err)
</script>

✅ 优点:支持 .xls.xlsx,保留原始样式,无需手动解析。

⚠️ 注意:需确保文件来源支持跨域,或通过后端代理获取二进制流 ‌27。

相关推荐
猫猫不是喵喵.1 小时前
Vue3 Props 属性
前端·javascript·vue.js
jarvisuni6 小时前
DeepSeekFlash前端依旧拉垮,而且变慢了很多!
前端·javascript·算法
破z晓8 小时前
javascript 导出excel表
开发语言·javascript·excel
小罗水9 小时前
第17章 后台管理端与演示支持
javascript·vue.js·ecmascript
万少10 小时前
DeepSeek-V4-Flash 正式版上线了,但这 3 个坑我帮你提前踩了
前端·javascript·后端
宁风NF11 小时前
JavaScript:网络请求与前端通信
开发语言·前端·javascript·网络·学习·ecmascript
吾儿良辰11 小时前
Flutter 迎来重大变革:Material 设计系统拆分,迈向更开放的 UI 框架
javascript·flutter·ui
猫猫不是喵喵.11 小时前
Vue3 中 computed 计算属性与 watch、watchEffect 监听
前端·javascript·vue.js
En^_^Joy11 小时前
Vue项目创建与入口配置全攻略
前端·vue.js·arcgis
Am-Chestnuts12 小时前
豆包多轮回答中的表格导出 Excel:编号、日期与长数字检查
excel