React實(shí)現(xiàn)預(yù)覽展示docx和Excel文件
React預(yù)覽docx文件
封裝DocxView組件,用于顯示docx文件的預(yù)覽,支持加載loading效果
安裝依賴
npm i docx-preview
import React, { useEffect, useRef, useState } from 'react' import * as docx from 'docx-preview' import { Spin } from 'antd' import { askDocApiUrls } from 'src/shared/url-map' export interface Props { fileInfo: string } const DocxView = (props: Props) => { const { fileInfo } = props const [isLoading, setIsLoading] = useState<boolean>(true) const docxContainerRef = useRef<HTMLDivElement | null>(null) useEffect(() => { const fetchData = async () => { try { const response = await fetch(fileInfo) const data = await response.blob() const containerElement = docxContainerRef.current if (containerElement) { docx.renderAsync(data, containerElement).then(() => { console.info('docx: finished') setIsLoading(false) }) } } catch (error) { setIsLoading(false) console.error('Error fetching or rendering document:', error) } } fetchData() }, [fileInfo]) return ( <div className="relative h-full"> <div ref={docxContainerRef} 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 DocxView
React預(yù)覽展示excel文件
封裝了ExcelView來展示excel文件,支持顯示loading
1.安裝依賴
npm i @js-preview/excel
2.源碼
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) // 開始加載時設(shè)置 loading 狀態(tài) myExcelPreviewer .preview(fileInfo) .then(() => { setIsLoading(false) // 預(yù)覽完成后取消 loading 狀態(tài) console.info('預(yù)覽完成') }) .catch((e) => { setIsLoading(false) // 預(yù)覽失敗后取消 loading 狀態(tài) console.info('預(yù)覽失敗', 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
以上就是React實(shí)現(xiàn)預(yù)覽展示docx和Excel文件的詳細(xì)內(nèi)容,更多關(guān)于React預(yù)覽文件的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
react PropTypes校驗(yàn)傳遞的值操作示例
這篇文章主要介紹了react PropTypes校驗(yàn)傳遞的值操作,結(jié)合實(shí)例形式分析了react PropTypes針對傳遞的值進(jìn)行校驗(yàn)操作相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2020-04-04React-View-UI組件庫封裝Loading加載中源碼
這篇文章主要介紹了React-View-UI組件庫封裝Loading加載樣式,主要包括組件介紹,組件源碼及組件測試源碼,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-06-06關(guān)于React中setState同步或異步問題的理解
相信很多小伙伴們都一直在疑惑,setState 到底是同步還是異步。本文就詳細(xì)的介紹一下React中setState同步或異步問題,感興趣的可以了解一下2021-11-11React Native 集成 iOS 原生功能(打印機(jī)功能為例)
在 React Native 項(xiàng)目中集成 iOS 原生功能是一個常見需求,本文將同樣以打印機(jī)功能為例,詳細(xì)介紹如何在 React Native 項(xiàng)目中集成 iOS 原生功能,感興趣的朋友一起看看吧2024-12-12react如何利用useRef、forwardRef、useImperativeHandle獲取并處理dom
這篇文章主要介紹了react如何利用useRef、forwardRef、useImperativeHandle獲取并處理dom,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2023-10-10react ant-design Select組件下拉框map不顯示的解決
這篇文章主要介紹了react ant-design Select組件下拉框map不顯示的解決方案,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-03-03React-Native做一個文本輸入框組件的實(shí)現(xiàn)代碼
這篇文章主要介紹了React-Native做一個文本輸入框組件的實(shí)現(xiàn)代碼,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-08-08