Import and export
The APIs on this page require a conversion backend. Follow file exchange integration to connect uploads, conversion, task queries, and downloads, then configure the browser plugins. Snapshot APIs also require that backend, but they do not require a collaborative document. The collaboration Exchange example demonstrates Sheets, so verify that the target release supports the required file types.
If an export fails, see license error codes and task failure handling.
Imported Office attachments are described in OLE previews, online conversion, and export source selection.
Supported formats
The table describes the browser APIs on this page. Office file conversion also requires a backend that supports the same document type and format.
| Import | Export |
|---|---|
.doc, .docx | .docx |
Both .doc and .docx use the import APIs below. Export always produces .docx; the legacy .doc format cannot be exported.
Facade API
Importing
Plugin mode note
Only plugin mode requires importing the Facade package manually. Preset mode already includes the corresponding Facade package, so no additional import is needed.
import '@univerjs-pro/exchange-client/facade'import '@univerjs-pro/docs-exchange-client/facade'Importing .docx
Import .docx file and get unitId
Caution
This method requires collaboration, because it creates a document on the server and returns a unitId. If collaboration is not enabled, use snapshot import.
When collaborative editing is enabled, each document has a unique unitId. Call univerAPI.importDocToUnitIdAsync with the file parameter, and it returns the unitId, which is then used to access the document. The file parameter accepts a File object or a remote file URL.
import '@univerjs-pro/collaboration-client/facade'const unitId = await univerAPI.importDocToUnitIdAsync(file)if (unitId) { await univerAPI.getCollaboration().loadDocAsync(unitId)}Import .docx file and get IDocumentData
unitId import when the document requires collaboration.univerAPI.importDocToSnapshotAsync imports a .docx file and returns the document data as IDocumentData.
// Accept a File objectconst snapshot = await univerAPI.importDocToSnapshotAsync(file)// Or accept a remote file URL// const snapshot = await univerAPI.importDocToSnapshotAsync('https://example.com/filename.docx');// Create a new document via snapshotif (snapshot) univerAPI.createDocument(snapshot)Exporting .docx
Export .docx file using unitId
Caution
unitId export requires collaboration. If collaboration is not enabled, use snapshot export.
When collaborative editing is enabled, each document has a unique unitId. Call univerAPI.exportDocByUnitIdAsync with the unitId parameter to receive a File object.
const fDocument = univerAPI.getActiveDocument()const unitId = fDocument.getId()const file = await univerAPI.exportDocByUnitIdAsync(unitId)// Download the file through the Facade APIif (file) univerAPI.downloadFile(file, 'univer', 'docx')Export .docx file using IDocumentData
univerAPI.exportDocBySnapshotAsync accepts IDocumentData and returns a File object.
const fDocument = univerAPI.getActiveDocument()const snapshot = fDocument.save()const file = await univerAPI.exportDocBySnapshotAsync(snapshot)// Download the file through the Facade APIif (file) univerAPI.downloadFile(file, 'univer', 'docx')Removal of built-in import/export menus and UI interactions
Note
Starting with version 1.2.0, Univer no longer includes built-in import/export menus or UI interactions. The underlying services and the Facade APIs remain available.
If the application still requires those menus and UI interactions, the migration example for built-in import/export menus and UI interactions shows how to move them into a custom plugin. The example includes the complete source code, which can be viewed online or downloaded.
How is this guide?