news 2026/4/3 6:36:51

使用onlyoffice预览word、excel、ppt、pdf等,可以双击index.html看效果的demo示例

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
使用onlyoffice预览word、excel、ppt、pdf等,可以双击index.html看效果的demo示例

index.html

<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>OnlyOffice 文档预览测试</title> <script src="https://chat.xutongbao.top/onlyoffice/web-apps/apps/api/documents/api.js"></script> <style> * { margin: 0; padding: 0; box-sizing: border-box; } body { font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); min-height: 100vh; padding: 20px; } .container { max-width: 1400px; margin: 0 auto; } .header { background: white; border-radius: 20px; padding: 30px; margin-bottom: 20px; box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1); } .header h1 { font-size: 32px; color: #333; margin-bottom: 10px; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); -webkit-background-clip: text; -webkit-text-fill-color: transparent; background-clip: text; } .header p { color: #666; font-size: 14px; } .test-buttons { background: white; border-radius: 20px; padding: 30px; margin-bottom: 20px; box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1); } .test-buttons h2 { font-size: 20px; color: #333; margin-bottom: 20px; display: flex; align-items: center; gap: 10px; } .buttons-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 15px; } .test-btn { border: 2px solid transparent; border-radius: 15px; padding: 20px; cursor: pointer; transition: all 0.3s ease; text-align: center; background: linear-gradient(white, white) padding-box, linear-gradient(135deg, currentColor, currentColor) border-box; position: relative; overflow: hidden; } .test-btn:hover { transform: translateY(-5px); box-shadow: 0 15px 40px rgba(0, 0, 0, 0.15); } .test-btn.word { color: #2b579a; border-color: rgba(43, 87, 154, 0.3); } .test-btn.word:hover { background: linear-gradient(135deg, rgba(43, 87, 154, 0.1), rgba(43, 87, 154, 0.05)); } .test-btn.excel { color: #1d6f42; border-color: rgba(29, 111, 66, 0.3); } .test-btn.excel:hover { background: linear-gradient(135deg, rgba(29, 111, 66, 0.1), rgba(29, 111, 66, 0.05)); } .test-btn.powerpoint { color: #c43e1c; border-color: rgba(196, 62, 28, 0.3); } .test-btn.powerpoint:hover { background: linear-gradient(135deg, rgba(196, 62, 28, 0.1), rgba(196, 62, 28, 0.05)); } .test-btn.pdf { color: #dc2626; border-color: rgba(220, 38, 38, 0.3); } .test-btn.pdf:hover { background: linear-gradient(135deg, rgba(220, 38, 38, 0.1), rgba(220, 38, 38, 0.05)); } .icon { width: 48px; height: 48px; margin: 0 auto 15px; border-radius: 12px; display: flex; align-items: center; justify-content: center; font-size: 24px; color: white; transition: transform 0.3s ease; } .test-btn:hover .icon { transform: scale(1.1); } .test-btn.word .icon { background: linear-gradient(135deg, #2b579a, #1e3c6e); } .test-btn.excel .icon { background: linear-gradient(135deg, #1d6f42, #15562f); } .test-btn.powerpoint .icon { background: linear-gradient(135deg, #c43e1c, #9a2f14); } .test-btn.pdf .icon { background: linear-gradient(135deg, #dc2626, #b91c1c); } .btn-title { font-size: 16px; font-weight: 600; margin-bottom: 5px; } .btn-desc { font-size: 12px; color: #666; } .preview-area { background: white; border-radius: 20px; overflow: hidden; box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1); display: none; } .preview-area.active { display: block; } .file-info { background: white; border-radius: 20px; padding: 20px 30px; margin-bottom: 20px; box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1); display: none; align-items: center; justify-content: space-between; } .file-info.active { display: flex; } .file-info-left { display: flex; align-items: center; gap: 15px; flex: 1; } .file-icon { width: 48px; height: 48px; border-radius: 12px; display: flex; align-items: center; justify-content: center; font-size: 20px; color: white; background: linear-gradient(135deg, #10b981, #059669); } .file-details h3 { font-size: 18px; color: #333; margin-bottom: 5px; } .file-meta { font-size: 12px; color: #666; display: flex; align-items: center; gap: 10px; } .close-btn { padding: 8px 16px; border: 2px solid #e5e7eb; border-radius: 10px; background: white; color: #666; cursor: pointer; font-size: 14px; transition: all 0.3s ease; } .close-btn:hover { border-color: #dc2626; background: rgba(220, 38, 38, 0.05); color: #dc2626; } #onlyoffice-editor { width: 100%; height: calc(100vh - 300px); min-height: 700px; } .loading { display: flex; flex-direction: column; align-items: center; justify-content: center; height: calc(100vh - 300px); min-height: 700px; background: #f9fafb; } .spinner { width: 50px; height: 50px; border: 4px solid #e5e7eb; border-top-color: #667eea; border-radius: 50%; animation: spin 1s linear infinite; } @keyframes spin { to { transform: rotate(360deg); } } .loading-text { margin-top: 20px; font-size: 18px; color: #333; font-weight: 600; } .error-message { background: #fee2e2; border: 2px solid #fca5a5; border-radius: 15px; padding: 20px; margin-top: 20px; color: #dc2626; display: none; } .error-message.active { display: block; } .supported-formats { background: rgba(102, 126, 234, 0.05); border: 1px solid rgba(102, 126, 234, 0.2); border-radius: 15px; padding: 15px; margin-top: 15px; } .format-title { font-size: 14px; font-weight: 600; color: #333; margin-bottom: 10px; display: flex; align-items: center; gap: 8px; } .formats-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 8px; font-size: 12px; color: #666; } .format-item { display: flex; align-items: center; gap: 5px; } .format-label { font-weight: 600; color: #333; } iframe { height: 500px; } </style> </head> <body> <div class="container"> <!-- 标题区域 --> <div class="header"> <h1>📄 OnlyOffice 文档预览测试</h1> <p>支持 Word、Excel、PowerPoint 等多种格式的文档在线预览</p> <!-- 支持格式说明 --> <div class="supported-formats"> <div class="format-title"> ✨ 支持的文件格式 </div> <div class="formats-grid"> <div class="format-item"> <span class="format-label">文档:</span> <span>DOC, DOCX, ODT, RTF, TXT, PDF 等</span> </div> <div class="format-item"> <span class="format-label">表格:</span> <span>XLS, XLSX, ODS, CSV 等</span> </div> <div class="format-item"> <span class="format-label">演示:</span> <span>PPT, PPTX, ODP 等</span> </div> </div> </div> </div> <!-- 测试按钮区域 --> <div class="test-buttons"> <h2>🚀 快速测试</h2> <div class="buttons-grid"> <button class="test-btn word" onclick="loadDocument('word')"> <div class="icon">📝</div> <div class="btn-title">测试 Word</div> <div class="btn-desc">文档编辑器</div> </button> <button class="test-btn excel" onclick="loadDocument('excel')"> <div class="icon">📊</div> <div class="btn-title">测试 Excel</div> <div class="btn-desc">表格编辑器</div> </button> <button class="test-btn powerpoint" onclick="loadDocument('powerpoint')"> <div class="icon">📽️</div> <div class="btn-title">测试 PowerPoint</div> <div class="btn-desc">演示文稿编辑器</div> </button> <button class="test-btn pdf" onclick="loadDocument('pdf')"> <div class="icon">📄</div> <div class="btn-title">测试 PDF</div> <div class="btn-desc">PDF 阅读器</div> </button> </div> </div> <!-- 错误提示 --> <div class="error-message" id="errorMessage"></div> <!-- 文件信息 --> <div class="file-info" id="fileInfo"> <div class="file-info-left"> <div class="file-icon">📄</div> <div class="file-details"> <h3 id="fileName">文档名称</h3> <div class="file-meta"> <span id="fileSize">大小</span> <span>•</span> <span id="fileType">类型</span> <span>•</span> <span id="fileTime">时间</span> </div> </div> </div> <button class="close-btn" onclick="closePreview()">✕ 关闭</button> </div> <!-- 预览区域 --> <div class="preview-area" id="previewArea"> <div id="onlyoffice-editor"></div> </div> </div> <script> let docEditor = null; // 文档配置 const documents = { word: { url: 'https://static.xutongbao.top/ai/onlyoffice/1769051991957_%E6%B5%8B%E8%AF%95%E4%B8%80%E4%B8%8B.docx', documentType: 'word', fileName: '测试文档.docx', fileType: 'docx', size: '240 KB', icon: '📝' }, excel: { url: 'https://static.xutongbao.top/ai/onlyoffice/1769053651678_1.xlsx', documentType: 'cell', fileName: '测试表格.xlsx', fileType: 'xlsx', size: '100 KB', icon: '📊' }, powerpoint: { url: 'https://static.xutongbao.top/ai/onlyoffice/1769053694718_1.pptx', documentType: 'slide', fileName: '测试演示文稿.pptx', fileType: 'pptx', size: '500 KB', icon: '📽️' }, pdf: { url: 'https://static.xutongbao.top/ai/onlyoffice/1769054138134_%E6%B5%8B%E8%AF%95%E4%B8%80%E4%B8%8B.pdf', documentType: 'word', fileName: '测试文档.pdf', fileType: 'pdf', size: '1 MB', icon: '📄' } }; // 加载文档 function loadDocument(type) { const doc = documents[type]; if (!doc) { showError('未找到对应的文档配置'); return; } // 隐藏错误提示 hideError(); // 销毁旧的编辑器 if (docEditor) { try { docEditor.destroyEditor(); docEditor = null; } catch (error) { console.warn('销毁旧编辑器失败:', error); } } // 清空容器 const container = document.getElementById('onlyoffice-editor'); container.innerHTML = '<div class="loading"><div class="spinner"></div><div class="loading-text">正在加载预览...</div></div>'; // 显示文件信息 document.getElementById('fileName').textContent = doc.fileName; document.getElementById('fileSize').textContent = doc.size; document.getElementById('fileType').textContent = doc.documentType === 'word' ? 'Word 文档' : doc.documentType === 'cell' ? 'Excel 表格' : doc.documentType === 'slide' ? 'PowerPoint 演示' : 'PDF 文档'; document.getElementById('fileTime').textContent = new Date().toLocaleTimeString(); document.querySelector('.file-icon').textContent = doc.icon; document.getElementById('fileInfo').classList.add('active'); // 显示预览区域 document.getElementById('previewArea').classList.add('active'); // 延迟初始化编辑器 setTimeout(() => { initOnlyOfficeEditor(doc); }, 100); } // 初始化 OnlyOffice 编辑器 function initOnlyOfficeEditor(doc) { if (!window.DocsAPI) { showError('OnlyOffice API 未加载,请检查网络连接'); return; } const container = document.getElementById('onlyoffice-editor'); container.innerHTML = ''; const config = { documentType: doc.documentType, document: { fileType: doc.fileType, key: `${Date.now()}-${Math.random().toString(36).substring(7)}`, title: doc.fileName, url: doc.url, }, editorConfig: { mode: 'edit', lang: 'zh-CN', user: { id: 'test-user', name: '测试用户', }, }, width: '100%', height: '100%', events: { onDocumentReady: () => { console.log('OnlyOffice 文档加载完成'); }, onAppReady: () => { console.log('OnlyOffice 应用已准备就绪'); }, onError: (event) => { console.error('OnlyOffice 错误:', event); showError('文档加载失败,请重试'); } }, }; try { docEditor = new window.DocsAPI.DocEditor('onlyoffice-editor', config); console.log('OnlyOffice 编辑器初始化成功'); } catch (error) { console.error('初始化编辑器失败:', error); showError('编辑器初始化失败: ' + error.message); } } // 关闭预览 function closePreview() { if (docEditor) { try { docEditor.destroyEditor(); docEditor = null; } catch (error) { console.warn('销毁编辑器失败:', error); } } document.getElementById('fileInfo').classList.remove('active'); document.getElementById('previewArea').classList.remove('active'); document.getElementById('onlyoffice-editor').innerHTML = ''; } // 显示错误 function showError(message) { const errorEl = document.getElementById('errorMessage'); errorEl.textContent = '❌ ' + message; errorEl.classList.add('active'); } // 隐藏错误 function hideError() { const errorEl = document.getElementById('errorMessage'); errorEl.classList.remove('active'); } // 检查 API 是否加载 window.addEventListener('load', () => { if (!window.DocsAPI) { console.warn('OnlyOffice API 未加载'); } else { console.log('OnlyOffice API 已加载'); } }); </script> </body> </html>
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/1 5:34:07

绝缘材料高压漏电起痕试验仪BLD-6000V

绝缘材料高压漏电起痕试验仪BLD-6000V&#xff1a;绝缘材料安全测试的全面指南 电痕化——电气安全的隐形威胁在潮湿、多尘或盐雾环境中&#xff0c;绝缘材料表面可能悄然形成导电碳化通道&#xff0c;这一过程称为漏电起痕&#xff08;Electrical Tracking&#xff09;。它并…

作者头像 李华
网站建设 2026/4/1 15:53:39

YOLOv8-Pose 姿态识别 RK3588 实战:从模型训练到 RKNN 部署,精度与推理速度双提升

文章目录 【YOLOv8-pose姿态识别部署至RK3588:模型训练到RKNN落地,让人体姿态分析精度与边缘推理速度双突破】 一、项目背景与技术选型:为何选择YOLOv8-pose+RK3588? 二、环境搭建:从代码仓库到硬件适配 1. 源码获取与工程结构 2. 依赖安装与硬件配置 三、YOLOv8-pose模型…

作者头像 李华
网站建设 2026/3/31 15:47:49

基于Java的居民用电智慧管理系统的设计与实现全方位解析:附毕设论文+源代码

1. 为什么这个毕设项目值得你 pick ? 居民用电智慧管理系统旨在解决传统管理模式下数据处理效率低下、信息不对称等问题。该系统通过会员管理模块精准记录用户资料&#xff0c;电表参数及抄表任务等多维度进行精细化管理和数据分析&#xff0c;实现了电费账单的自动化生成与异…

作者头像 李华
网站建设 2026/4/3 4:57:38

模块化多电平变换器MMC两种调制策略实现仿真分享

模块化多电平变换器MMC两种调制策略实现&#xff08;交流3000V-直流5000V整流&#xff09;仿真&#xff0c;单桥臂二十子模块&#xff0c;分别采用最近电平逼近NLM与载波移相调制CPS-PWM实现&#xff0c;仿真中使用环流抑制&#xff0c;NLM中采用快速排序&#xff0c;两个仿真动…

作者头像 李华
网站建设 2026/4/1 13:07:14

电子时钟设计

2 系统结构 整个电子时钟系统电路可分为五大部分&#xff1a;中央处理单元&#xff08;CPU&#xff09;、电源电路部分、显示部分、键盘输入部分、温度采集部分。 2&#xff0e;1 中央处理单元 CPU选用AT89C—2051对整个系统进行控制&#xff1a; 1&#xff09;它将定时数据输出…

作者头像 李华
网站建设 2026/3/30 10:58:02

红队工具箱:进攻性安全运营的必备工具与技术

免责声明&#xff1a; 本文仅用于教育目的。未经授权利用系统是非法行为&#xff0c;将受到法律制裁。保持道德。遵守法律。安全负责。 感谢各位阅读。享受愉快的道德黑客之旅&#xff01;如果喜欢我的工作&#xff0c;请支持我&#xff01; &#x1f525; 红队介绍 红队演…

作者头像 李华