【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

相关推荐
小小愿望1 小时前
前端无法获取响应头(如 Content-Disposition)的原因与解决方案
前端·后端
小小愿望1 小时前
项目启功需要添加SKIP_PREFLIGHT_CHECK=true该怎么办?
前端
烛阴1 小时前
精简之道:TypeScript 参数属性 (Parameter Properties) 详解
前端·javascript·typescript
海上彼尚2 小时前
使用 npm-run-all2 简化你的 npm 脚本工作流
前端·npm·node.js
没有羊的王K2 小时前
随想记-excel报表美化
excel
开发者小天2 小时前
为什么 /deep/ 现在不推荐使用?
前端·javascript·node.js
如白驹过隙3 小时前
cloudflare缓存配置
前端·缓存
excel3 小时前
JavaScript 异步编程全解析:Promise、Async/Await 与进阶技巧
前端
Jerry说前后端3 小时前
Android 组件封装实践:从解耦到架构演进
android·前端·架构
步行cgn4 小时前
在 HTML 表单中,name 和 value 属性在 GET 和 POST 请求中的对应关系如下:
前端·hive·html