File exchange integration
Add file import and export to your Univer application, with or without collaborative editing.
The editor cannot open an Excel file directly. The server first converts the file into Univer document data, and on export it converts the edited data back into an Excel file. Both directions use the server-side @univerjs-pro/exchange-node module. The browser uploads the file, opens the document, and downloads the result.
Without collaborative editing
In the typical scenario, a user uploads an Excel file, edits it on the page, and downloads the result as an Excel file.
- The browser uploads the file. The server converts the file into Univer document data and returns the data.
- The browser opens a spreadsheet from that data, ready for editing.
- On export, the browser sends the edited data to the server, and the server generates an Excel file for download.
This path does not involve a collaboration service. A snapshot in the browser APIs is the document data used here. Importing or exporting a snapshot still requires server-side conversion; to let users save their work and return to it later, the application must store that data itself.
Installing and using the conversion module is covered in Office file import and export. That page is also the starting point if an application only needs file processing in Node.js.
With collaborative editing
In this scenario, the user imports an Excel file, shares the document with colleagues, edits it with them, and exports it later.
- The server converts the uploaded file and saves the result as a collaborative document.
- The server returns a document ID. The browser opens the document by that ID, and other users join the document with the same ID.
- On export, the server reads the latest content of the document and generates an Excel file for download.
That document ID is the unitId in the browser APIs. Set up Collaboration integration first, then run the Exchange example. The example presents browser, server, and conversion code side by side.
Pending edits must synchronize before an export; otherwise the exported file can miss the latest changes. To read and convert collaborative documents, see the server-side Office Exchange section in extension modules.
Connect the import and export buttons
Once server-side conversion works, the next step is to connect it to the page.
- Applications that use Univer's menus or Facade APIs register the File Endpoint and Exchange Endpoint in the same way as the Exchange example, then configure the browser plugins.
- Applications that provide their own buttons call the application APIs: they open the document data or document ID returned by an import, and download the generated file after an export.
Browser configuration for each product is in Sheets, Docs, Slides, Boards, Bases, and PDFs. The collaboration example demonstrates Sheets. For the other products, see Office file import and export and check which formats the installed release supports.
Check the integration
Take a real file from the application through import, edit, and export. Open the result in an Office application and check the edits, formulas, images, and layout. With collaboration, also confirm that other users can open the same document and that the content survives a server restart.
Before deployment, limit upload sizes and handle conversion failures and timeouts. Uploads, task queries, and downloads all require access checks. See identity and authorization.
Plugin configuration
UniverExchangeClientPlugin
downloadEndpointUrlstringDefault:location.originBase URL for file downloads.uploadFileServerUrlstringDefault:'/universer-api/stream/file/upload'Endpoint that receives uploaded files.importServerUrlstringDefault:'/universer-api/exchange/{type}/import'Endpoint that starts an import. Keep the{type}placeholder.exportServerUrlstringDefault:'/universer-api/exchange/{type}/export'Endpoint that starts an export. Keep the{type}placeholder.getTaskServerUrlstringDefault:'/universer-api/exchange/task/{taskID}'Endpoint for task status. Keep the{taskID}placeholder.signUrlServerUrlstringDefault:'/universer-api/file/{fileID}/sign-url'Endpoint that signs file URLs. Keep the{fileID}placeholder.maxTimeoutTimenumberDefault:120000Maximum time to poll a conversion task, in milliseconds.
Configure OLE attachments
UniverDrawingPlugin
oleobjectSettings for OLE attachments.getLinkIOleConfig["getLink"]Builds the application link for an online attachment.attachmentIoobjectReads and writes the original attachment in application storage.upload(file: File, targetUnitId: string) => Promise<IBaseAttachment>Writes an attachment to application storage for this unit.read(attachment: IBaseAttachment, signal?: AbortSignal) => Promise<Uint8Array>Reads the bytes of an attachment from application storage.
previewOfficeIOleConfig["previewOffice"]Overrides the Office preview flow.createOfficePreviewIOleConfig["createOfficePreview"]Builds an Office preview with an application callback.
Add ole to the existing UniverDrawingPlugin registration before Exchange is registered. The route below belongs to the application and must point to the application's document UI; the optional second getLink argument carries the document type when needed. Exchange performs Office conversion and detects the collaboration state. Attachments keyed by a UUID also require ole.attachmentIo, with authorized upload(file, targetUnitId) and read(attachment, signal) callbacks. Office previews require the matching renderer or an application-provided previewOffice / createOfficePreview callback.
import { UniverDrawingPlugin } from '@univerjs/drawing'univer.registerPlugin(UniverDrawingPlugin, { ole: { getLink: (unitId) => `/documents/${encodeURIComponent(unitId)}`, },})Export failures and license limits
An export can fail at submit time or later, while the task is polled. A successful HTTP response alone does not mean the file is ready. Check the error.code in the protocol and the task state. The exchange client keeps server export errors in IExchangeService.requestState$, so a custom export UI can subscribe before it starts the operation.
import { IExchangeService } from '@univerjs-pro/exchange-client'import { ErrorCode } from '@univerjs/protocol'const exchange = univer.__getInjector().get(IExchangeService)const subscription = exchange.requestState$.subscribe(({ error }) => { if (!error || error.code === ErrorCode.OK) return console.error(error.code, error.message)})| ErrorCode | Action |
|---|---|
LICENSE_EXPORT_SIZE_EXCEEDED | Reduce the document or obtain a license with a larger export limit. |
LICENSE_EXPORT_QUOTA_EXCEEDED | Check the remaining export quota and its reset policy. |
LICENSE_CONCURRENCY_TIMEOUT | Retry later, when conversion capacity is available. |
LICENSE_VERSION_NOT_ALLOWED | Use a version covered by the server license. |
LICENSE_UNIT_TYPE_REJECTED | Check whether the server license covers the document type. |
LICENSE_POLICY_INVALID, LICENSE_USAGE_STATE_INVALID, LICENSE_USAGE_STORAGE_ERROR | Inspect the server license configuration, usage state, and storage. |
Match on the named ErrorCode values from @univerjs/protocol; do not parse translated message text. Keep the server's message for diagnostics. Network failures, malformed task responses, and polling timeouts can surface as a generic failure instead, and none of them prove that a license rejected the export. Call subscription.unsubscribe() when the editor is removed.
If the application deploys its own backend or proxy in front, that backend or proxy must pass the structured error response through and mark the task failed instead of leaving it pending. Raising maxTimeoutTime only gives polling more time. It does not add export quota or change what the license covers.
OLE attachments
Sheets, Docs, and Slides store imported OLE files separately from their preview images. Users can download an original file when it is available, rename an occurrence, and remove an attachment if they have permission. Image and PDF attachments can be previewed. Office previews go through Exchange JSON conversion into a temporary read-only editor, and opening a preview does not create a persisted child document. Native Office applications and macros never run.
Converting an Office attachment into an online document requires a collaborative host, edit permission, conversion support, and getLink. The conversion creates an independent document and stores its identity on the attachment, so later opens reuse that document. Editing the document does not overwrite the original attachment bytes. Missing, external, or unsupported attachments keep their status instead of turning into editable Office objects.
Exports use oleAttachmentSource: 'original' by default. Set it to 'online' to embed the current confirmed version of each converted online document in that one export; the stored originals are not rewritten. Edits must synchronize before an export, and the backend must authorize access to the referenced documents. Pass the option as the second argument to Docs and Slides export APIs, or as the fourth argument to Sheets export APIs. Snapshot variants accept it in the same position. See the export API.
For persisted attachments, Exchange submits fileID and the sourceUnitID that owns it. Before converting by reference, the import backend must validate how the two relate and check the caller's access. Polling a task can return progress.stage as reading, converting, creating, or done. For a custom conversion UI, see the attachment import and progress APIs.
How is this guide?