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

news/2024/7/15 17:34:21 标签: 前端, react.js, excel

excelloading_1">封装了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 (
    <div className="relative h-full">
      <div ref={excelContainerRef} className="h-full" />
      {isLoading && (
        <div className="absolute inset-0 flex items-center justify-center bg-white bg-opacity-75">
          <Spin size="large" />
        </div>
      )}
    </div>
  )
}

export default ExcelView


http://www.niftyadmin.cn/n/5360703.html

相关文章

c++阶梯之auto关键字与范围for

auto关键字&#xff08;c11&#xff09; 1. auto关键字的诞生背景 随着程序的逐渐复杂&#xff0c;程序代码中用到的类型也越来越复杂。譬如&#xff1a; 类型难以拼写&#xff1b;含义不明确容易出错。 比如下面一段代码&#xff1a; #include <string> #include &…

详解Skywalking 采集springboot 应用日志的方法(内附源码)

大家都知道Skywalking 的链路追踪功能非常强大&#xff0c;可以帮助用户深入了解应用程序中各个组件之间的依赖关系。在实际应用中&#xff0c;往往需要将链路追踪数据与日志数据结合起来进行综合分析。Skywalking 提供了 Trace Log 结合插件&#xff0c;可以帮助用户快速定位问…

Linux 磁盘空间占用率100%的排查

&#x1f4d1;前言 使用 Linux 操作系统时&#xff0c;可能会遇到磁盘空间不足的错误&#xff0c;这种错误通常会导致系统运行缓慢或崩溃。本文将介绍磁盘排查的方法。⛺️ &#x1f3ac;作者简介&#xff1a;大家好&#xff0c;我是青衿&#x1f947; ☁️博客首页&#xff1…

Visual Studio Code安装配置C/C++教程 (windows版,亲测可行)

一.下载 Visual Studio Code https://code.visualstudio.com/ 二.安装 选择想要安装的位置: 后面的点击下一步即可。 三.下载编译器MinGW vscode只是写代码的工具&#xff0c;使用编译器才能编译写的C/C程序&#xff0c;将它转为可执行文件。 MinGW下载链接&#xff1a;…

(非常全面的干货)Python接口自动化测试框架实战开发

一丶叙述 1.项目介绍 整个项目分为四个部分&#xff1a;接口基础丶接口开发丶Unittest与接口测试结合以及接口自动化框架从设计到开发 接口基础包括&#xff1a;HTTP接口 / 常见接口 / 接口工具 / 接口基础知识 接口开发&#xff1a;通过Django来开发get/post接口 Unittes…

Flink 1.18.1的基本使用

系统示例应用 /usr/local/flink-1.18.1/bin/flink run /usr/local/flies/streaming/SocketWindowWordCount.jar --port 9010nc -l 9010 asd asd sdfsf sdf sdfsdagd sdf单次统计示例工程 cd C:\Dev\IdeaProjectsmvn archetype:generate -DarchetypeGroupIdorg.apache.flink -…

【更新】中国统计摘要2023年(PDF+excel格式)

《中国统计摘要》是一部专为及时展现中国国民经济与社会发展状况所编辑的综合统计著作。2023版的《中国统计摘要》为我们呈现了2022年的重要社会经济指标数据&#xff0c;并同时为读者提供了自1978年以来的历史数据摘要。需要特别注意的是&#xff0c;为了确保这本书的及时性&a…

假期刷题打卡--Day20

1、MT1173魔数 一个数字&#xff0c;把他乘以二&#xff0c;会得到一个新的数字&#xff0c;如果这个新数字依然由原数中那些数字组成&#xff0c;就称原数为一个魔数。输入正整数N&#xff0c;检查它是否是一个魔数&#xff0c;输出YES或者NO。 格式 输入格式&#xff1a; …