news 2026/11/2 15:00:33

Vue文档编辑器(Word默认功能)示例

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
Vue文档编辑器(Word默认功能)示例

Spire.WordJS 基于 HTML5 标准,支持跨平台开发和集成,支持所有主流浏览器,无需安装任何插件或第三方组件,以原生的方式嵌入各类应用,可以与各类前端技术框架Vue、React、Angular 等相结合。 Spire.WordJS以纯前端、跨平台的能力,让您的应用系统轻松获得与Word,WPS文档一致的交互体验。

此示例为您完整演示Vue框架中如何使用Spire.WordJS文档编辑器创建、编辑并打印 Word 格式(DOCX)的文档:

<template> <div class="form"> <div id="iframeEditor"> </div> </div> </template> <script setup> import { ref, onMounted, onUnmounted } from 'vue'; import { storeToRefs } from 'pinia'; import { useFileStore } from '../stores/file.js' import { useRouter } from 'vue-router'; const fileStore = useFileStore() // Pinia 中当存储的数据 const { file, fileUint8Data } = storeToRefs(fileStore) const router = useRouter() const config = ref({}); const isOpened = ref(false); const editorInstance = ref(null); const apiInstance = ref(null); const originUrl = window.location.origin onMounted(() => { // 没有文件时直接跳回上传页 if (!file.value) { router.replace('/'); return; } // 使用script的方式载入 loadScript(); window.addEventListener('resize', OnWindowReSize); }) onUnmounted(() => { window.removeEventListener('resize', OnWindowReSize); }) // 初始化编辑器所需的配置对象 function initConfig() { if (!file.value) { throw new Error('未找到文件,请重新上传'); } if (!fileUint8Data.value) { throw new Error('未找到文件数据,请重新上传'); } config.value = { "fileAttrs": { "fileInfo": { "name": file.value.name, "ext": getFileExtension(), "primary": String(new Date().getTime()), "creator": "Jonn", "createTime": "2022-04-18 11:30:43" }, "sourceUrl": originUrl + "/files/__ffff_192.168.2.134/" + file.value.name, "createUrl": originUrl + "/open", "mergeFolderUrl": "", "fileChoiceUrl": "", "templates": {} }, "user": { "id": "uid-1", "name": "Jonn", "canSave": true, }, "editorAttrs": { "editorMode": "edit", //edit / view "editorWidth": "100%", "editorHeight": "100%", "editorType": "document", "platform": "desktop", //desktop / mobile / embedded "viewLanguage": "zh", //en / zh "isReadOnly": false, "canChat": true, "canComment": true, "canReview": true, "canDownload": true, "canEdit": true, "canForcesave": true, "embedded": { "saveUrl": "", "embedUrl": "", "shareUrl": "", "toolbarDocked": "top" }, "useWebAssemblyDoc": true, "useWebAssemblyExcel": true, "useWebAssemblyPpt": true, "spireDocJsLicense": "", "spireXlsJsLicense": "", "spirePresentationJsLicense": "", "spirePdfJsLicense": "", "serverless": { "useServerless": true, "baseUrl": originUrl, "fileData": fileUint8Data.value, }, "events": { "onSave": onFileSave }, "plugins": { "pluginsData": [] } } }; } // 创建并渲染 SpireCloudEditor 实例 function initEditor() { let iframeId = 'iframeEditor'; initConfig(); isOpened.value = true; editorInstance.value = new SpireCloudEditor.OpenApi(iframeId, config.value); // 创建编辑器实例 window.Api = apiInstance.value = editorInstance.value.GetOpenApi(); // 暴露 OpenApi 便于调试/保存 OnWindowReSize(); } // 获取上传文件的扩展名,用于配置 fileInfo.ext function getFileExtension() { const filename = file.value.name.split(/[\\/]/).pop(); // 获取最后一个点后的内容 return filename.substring(filename.lastIndexOf('.') + 1).toLowerCase() || ''; } // 调整编辑器容器尺寸,使其随窗口大小自适应 function OnWindowReSize() { let wrapEl = document.getElementsByClassName("form"); if (wrapEl.length) { wrapEl[0].style.height = screen.availHeight + "px"; window.scrollTo(0, -1); wrapEl[0].style.height = window.innerHeight + "px"; } } // 动态加载 SpireCloudEditor 脚本,避免重复注入 function loadScript() { if (window.SpireCloudEditor) { initEditor() return } const script = document.createElement('script'); script.setAttribute('src', '/spire.cloud/web/editors/spireapi/SpireCloudEditor.js'); script.onload = () => initEditor() document.head.appendChild(script); } // Spire 编辑器的保存回调,可接入自定义保存逻辑 function onFileSave(data) { console.log('save data', data) } </script> <style> .form, iframe, body { min-height: 100vh !important; min-width: 100vh !important; } </style>

Spire.WordJS文档编辑器界面选项清晰易懂,遵循Microsoft Word 和WPS通用规则,您可以快速在线完成 Word 文档的创建, 编辑, 打印及保存等。Spire.WordJS的每个独立功能均设为独立模块,可协助您搭建满足自身需求的轻量 Word 编辑工具。

Spire.WordJS文档编辑器核心功能:

  • 创建与编辑:打开和保存文档。
  • 支持的文档格式:.doc, .docx, .wps, .wpt, .docm, .dotm 等
  • 支持元素:文本、图片、超链接、表格、书签、页码、目录、页眉页脚等。
  • 格式设置:文本层级、段落层级、项目符号与编号、表格层级、页面设置及样式。
  • 编辑操作:撤销、重做、剪切、复制、粘贴。
  • 文档内文本查找替换。

如需要更多详细资料,请留言

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/11/1 22:32:00

高效金融数据采集秘诀:零基础掌握pywencai股票数据接口实战指南

高效金融数据采集秘诀&#xff1a;零基础掌握pywencai股票数据接口实战指南 【免费下载链接】pywencai 获取同花顺问财数据 项目地址: https://gitcode.com/gh_mirrors/py/pywencai 你是否曾遇到这样的困境&#xff1a;想通过数据分析挖掘股票市场机会&#xff0c;却被复…

作者头像 李华
网站建设 2026/11/1 19:13:40

ComfyUI-Florence2模型加载故障深度排查与解决方案

ComfyUI-Florence2模型加载故障深度排查与解决方案 【免费下载链接】ComfyUI-Florence2 Inference Microsoft Florence2 VLM 项目地址: https://gitcode.com/gh_mirrors/co/ComfyUI-Florence2 问题现象&#xff1a;Florence2节点消失与目录错误 当用户在ComfyUI中安装F…

作者头像 李华
网站建设 2026/11/2 2:59:55

2048 AI助手:让智能算法助你轻松成为数字合并大师

2048 AI助手&#xff1a;让智能算法助你轻松成为数字合并大师 【免费下载链接】2048-ai AI for the 2048 game 项目地址: https://gitcode.com/gh_mirrors/20/2048-ai 还在为2048游戏中怎么才能合成更大的数字而烦恼吗&#xff1f;别担心&#xff01;这款2048 AI助手来啦…

作者头像 李华
网站建设 2026/11/2 3:27:05

verl客服机器人集成:智能对话训练实战

verl客服机器人集成&#xff1a;智能对话训练实战 1. verl 是什么&#xff1a;专为大模型对话优化的强化学习框架 你可能已经用过不少大模型训练工具&#xff0c;但真正能兼顾灵活性、生产可用性和训练效率的框架并不多。verl 就是这样一个少有的“三边都稳”的选择——它不是…

作者头像 李华
网站建设 2026/11/2 3:26:51

Qwen-Image-Edit-2511 wireframe生成,设计师的好帮手

Qwen-Image-Edit-2511 wireframe生成&#xff0c;设计师的好帮手 你有没有过这样的经历&#xff1a;刚画完一个产品草图&#xff0c;客户突然说“能不能加个线框风格&#xff1f;我要给工程师看结构”&#xff1b;或者正在做UI原型&#xff0c;团队要求快速输出Blender式线框稿…

作者头像 李华