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。

相关推荐
前端小菜鸟也有人起2 小时前
vue中is的作用和用法
前端·javascript·vue.js
酉鬼女又兒2 小时前
零基础入门前端JavaScript 基础语法详解(可用于备赛蓝桥杯Web应用开发)
开发语言·前端·javascript·chrome·蓝桥杯
该怎么办呢2 小时前
packages\engine\Source\Core\Cartesian3.js
前端·javascript·cesium
颜酱2 小时前
吃透回溯算法:从框架到实战
javascript·后端·算法
changhong19862 小时前
Docker部署Spring Boot + Vue项目
vue.js·spring boot·docker
哈哈哈hhhhhh2 小时前
vue----v-model
前端·javascript·vue.js
QD_ANJING2 小时前
2026年大厂前端高频面试原题-React框架200题
开发语言·前端·javascript·react.js·面试·职场和发展·前端框架
24白菜头3 小时前
若依框架Ruoyi-Vue-SpringBoot3部署
前端·javascript·笔记·后端·学习
光影少年3 小时前
react的diff算法和vue的diff算法区别
vue.js·算法·react.js