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。

相关推荐
kyriewen4 小时前
我把最常踩的8个CORS跨域报错整理了一遍——第8个去年还不存在
前端·javascript
AI英德西牛仔7 小时前
从Excel乱象到批量交付:秘塔Excel与“AI导出鸭”PC版的底层重构逻辑
人工智能·重构·excel·deepseek·ai导出鸭
NeilCarmack8 小时前
Deepseek-harness增加桌面版端序列:第 2 讲 · spawn Electron:当前进程如何“交棒“
前端·javascript·electron
上海魁鲸科技有限公司9 小时前
APS高级排产系统到底有什么用?一文讲清功能、选型与落地建议
前端·microsoft·excel
এ慕ོ冬℘゜11 小时前
使用 jQuery 动态渲染表格与状态切换
前端·javascript·jquery
智购科技自动售货机厂家13 小时前
2026自动售货机AI视觉识别优化:从YOLOv11n模型量化到RKNN部署的端侧推理工程实践~YH
javascript·人工智能·yolo·机器学习·计算机视觉·目标跟踪·perl
晓说前端13 小时前
TypeScript 核心语法应用 —— Vue 3 中的使用(下)
前端·javascript·typescript·类型系统
lancyu15 小时前
# Agent Loop:AI Agent 的唯一直心骨
开发语言·javascript·人工智能
半个落月16 小时前
JavaScript 单例模式详解:从唯一实例到按钮事件
javascript