【React】前端React 代码中预览展示excel文件

封装了ExcelView来展示excel文件,支持显示loading

1.安装依赖

复制代码
npm i @js-preview/excel
  1. 源码

    import React, { useEffect, useRef, useState } from 'react'
    import jsPreviewExcel, { JsExcelPreview } from '@js-preview/excel'
    import '@js-preview/excel/lib/index.css'
    import { Spin } from 'antd'

    export interface Props {
    fileInfo: string
    }

    const ExcelView = (props: Props) => {
    const { fileInfo } = props
    const excelContainerRef = useRef<HTMLDivElement | null>(null)
    const excelPreviewerRef = useRef<JsExcelPreview | null>(null) // 保存 myExcelPreviewer 的引用
    const [isLoading, setIsLoading] = useState<boolean>(true)

    useEffect(() => {
    const containerElement = excelContainerRef.current

    复制代码
     if (containerElement && !excelPreviewerRef.current) {
       // 初始化 myExcelPreviewer,并保存引用
       const myExcelPreviewer = jsPreviewExcel.init(containerElement)
       excelPreviewerRef.current = myExcelPreviewer
    
       setIsLoading(true) // 开始加载时设置 loading 状态
    
       myExcelPreviewer
         .preview(fileInfo)
         .then(() => {
           setIsLoading(false) // 预览完成后取消 loading 状态
           console.info('预览完成')
         })
         .catch((e) => {
           setIsLoading(false) // 预览失败后取消 loading 状态
           console.info('预览失败', e)
         })
     }

    }, [fileInfo])

    return (



    {isLoading && (

    <Spin size="large" />

    )}

    )
    }

    export default ExcelView

相关推荐
大怪v3 小时前
AI抢饭?前端佬:我要验牌!
前端·人工智能·程序员
新酱爱学习3 小时前
字节外包一年,我的技术成长之路
前端·程序员·年终总结
小兵张健3 小时前
开源 playwright-pool 会话池来了
前端·javascript·github
IT_陈寒6 小时前
Python开发者必知的5大性能陷阱:90%的人都踩过的坑!
前端·人工智能·后端
codingWhat6 小时前
介绍一个手势识别库——AlloyFinger
前端·javascript·vue.js
代码老中医6 小时前
2026年CSS彻底疯了:这6个新特性让我删掉了三分之一JS代码
前端
不会敲代码16 小时前
Zustand:轻量级状态管理,从入门到实践
前端·typescript
踩着两条虫6 小时前
VTJ.PRO 双向代码转换原理揭秘
前端·vue.js·人工智能
扉川川6 小时前
OpenClaw 架构解析:一个生产级 AI Agent 是如何设计的
前端·人工智能
远山枫谷6 小时前
一文理清页面/组件通信与 Store 全局状态管理
前端·微信小程序