React實現(xiàn)預覽展示docx和Excel文件
更新時間:2024年02月03日 16:14:26 作者:郭_昊
這篇文章主要為大家詳細介紹了如何使用React實現(xiàn)預覽展示docx和Excel文件,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學習一下
React預覽docx文件
封裝DocxView組件,用于顯示docx文件的預覽,支持加載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預覽展示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) // 開始加載時設置 loading 狀態(tài) myExcelPreviewer .preview(fileInfo) .then(() => { setIsLoading(false) // 預覽完成后取消 loading 狀態(tài) console.info('預覽完成') }) .catch((e) => { setIsLoading(false) // 預覽失敗后取消 loading 狀態(tài) 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
以上就是React實現(xiàn)預覽展示docx和Excel文件的詳細內(nèi)容,更多關于React預覽文件的資料請關注腳本之家其它相關文章!
相關文章
React-View-UI組件庫封裝Loading加載中源碼
這篇文章主要介紹了React-View-UI組件庫封裝Loading加載樣式,主要包括組件介紹,組件源碼及組件測試源碼,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-06-06React Native 集成 iOS 原生功能(打印機功能為例)
在 React Native 項目中集成 iOS 原生功能是一個常見需求,本文將同樣以打印機功能為例,詳細介紹如何在 React Native 項目中集成 iOS 原生功能,感興趣的朋友一起看看吧2024-12-12react如何利用useRef、forwardRef、useImperativeHandle獲取并處理dom
這篇文章主要介紹了react如何利用useRef、forwardRef、useImperativeHandle獲取并處理dom,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧2023-10-10react ant-design Select組件下拉框map不顯示的解決
這篇文章主要介紹了react ant-design Select組件下拉框map不顯示的解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-03-03React-Native做一個文本輸入框組件的實現(xiàn)代碼
這篇文章主要介紹了React-Native做一個文本輸入框組件的實現(xiàn)代碼,非常具有實用價值,需要的朋友可以參考下2017-08-08