Import and export
The APIs below need a conversion backend. Follow file exchange integration to connect uploads, conversion, task queries, and downloads, then configure the browser plugins. Snapshot APIs also need that backend, but do not require a collaborative document. The collaboration Exchange example demonstrates Sheets; check that your target release supports the file types you need.
For export failures, see license error codes and task failure handling.
Supported formats
The table describes the browser APIs on this page. Office file conversion 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 produces .docx; exporting the legacy .doc format is not supported.
Facade API
Importing
Plugin mode note
Only plugin mode requires manually importing the Facade package. Preset mode already includes the corresponding Facade package, so no extra 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: it creates a server document and returns a unitId. Use snapshot import if
collaboration is not enabled.
When collaborative editing is enabled, each document has a unique unitId. Using univerAPI.importDocToUnitIdAsync with the file parameter will return the unitId, which can be used to access the document. The file parameter can be 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 for collaborative documents.Using univerAPI.importDocToSnapshotAsync to import a .docx file will return the document data in the IDocumentData format.
// 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. Using univerAPI.exportDocByUnitIdAsync with the unitId parameter will return 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
Using univerAPI.exportDocBySnapshotAsync with IDocumentData will return 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 and UI interactions. The underlying services and Facade APIs remain available.
If your application needs to retain the existing import/export menus and UI interactions, see the migration example for built-in import/export menus and UI interactions to migrate using a custom plugin. The example provides complete source code for online viewing and download.
How is this guide?