Import and export
If an export fails, see license error codes and task failure handling.
Bases imports XLSX, XLS, CSV, and TSV and exports XLSX, CSV, and TSV. A common flow is to import an Excel customer list, add fields and views, and then export the table data again.
These APIs require a conversion backend. Complete file exchange integration first and confirm that the backend supports the Bases document type and the required formats.
Supported formats
The table covers the browser APIs described on this page. Converting Office files still requires a backend that supports the same document type and format.
| Import | Export |
|---|---|
.xls, .xlsx, .csv, .tsv | .xlsx, .csv, .tsv |
CSV and TSV export a single table, which is selected with tableId. Neither format preserves views, permissions, or any other Base resource. For a complete backup of the application, save a Base snapshot instead.
Install and register
Install these packages in an initialized Bases editor:
pnpm add @univerjs-pro/exchange-client @univerjs-pro/bases-exchange-clientnpm install @univerjs-pro/exchange-client @univerjs-pro/bases-exchange-clientyarn add @univerjs-pro/exchange-client @univerjs-pro/bases-exchange-clientbun add @univerjs-pro/exchange-client @univerjs-pro/bases-exchange-clientimport { UniverBasesExchangeClientPlugin } from '@univerjs-pro/bases-exchange-client'import { UniverExchangeClientPlugin } from '@univerjs-pro/exchange-client'import '@univerjs-pro/bases/facade'import '@univerjs-pro/bases-exchange-client/facade'import '@univerjs-pro/exchange-client/facade'univer.registerPlugin(UniverExchangeClientPlugin, exchangeConfig)univer.registerPlugin(UniverBasesExchangeClientPlugin)exchangeConfig contains the application's upload, conversion, task-query, and download settings. The integration guide describes how to prepare it.
UniverExchangeClientPlugin
downloadEndpointUrlstringDefault:location.originBase URL for file downloads.uploadFileServerUrlstringDefault:'/universer-api/stream/file/upload'Endpoint that receives uploads.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 that reports task status. Keep the{taskID}placeholder.signUrlServerUrlstringDefault:'/universer-api/file/{fileID}/sign-url'Endpoint that signs file URLs. Keep the{fileID}placeholder.maxTimeoutTimenumberDefault:120000Maximum polling time for a conversion task, in milliseconds.
Configure Excel conversion
UniverBasesExchangeClientPlugin
importSourceModeExchangeBaseImportModeDefault:AUTOWhether to import Excel tables, worksheets, or both.importFormulaPolicyExchangeBaseFormulaPolicyDefault:CONVERT_THEN_VALUESHow to treat formulas on import.exportStructureModeExchangeBaseExportModeDefault:TABLESWhether to export native Excel tables or worksheet ranges.exportFormulaPolicyExchangeBaseFormulaPolicyDefault:FAILHow to treat formulas on export.
To change the conversion behavior, pass options to the Bases exchange plugin. The values below are the defaults, and this registration replaces the one above:
import { ExchangeBaseExportMode, ExchangeBaseFormulaPolicy, ExchangeBaseImportMode,} from '@univerjs-pro/exchange-client'univer.registerPlugin(UniverBasesExchangeClientPlugin, { importSourceMode: ExchangeBaseImportMode.AUTO, importFormulaPolicy: ExchangeBaseFormulaPolicy.CONVERT_THEN_VALUES, exportStructureMode: ExchangeBaseExportMode.TABLES, exportFormulaPolicy: ExchangeBaseFormulaPolicy.FAIL,})- Import source:
AUTOimports Excel tables and visible worksheets automatically.TABLESimports Excel tables only,SHEETSimports visible worksheets, andHYBRIDimports the Excel tables plus any visible worksheet that has no corresponding Excel table. - Export structure:
TABLESconverts non-empty Base tables into native Excel tables and leaves empty tables as ordinary ranges.RANGESwrites every table as an ordinary worksheet range. - Formulas: during import, Univer converts the formulas it can and falls back to cached results for formulas that the engine does not support. During export, conversion must succeed unless the policy is changed:
VALUESwrites cached results only, andTEXTkeeps the source formula as text.CONVERT_THEN_VALUESstill fails on export when a formula cannot be converted.
Import for editing on the page
The file argument can be a File selected by the user or a URL pointing to a remote file. The call returns a snapshot of Base data that the editor can load:
const snapshot = await univerAPI.importBaseToSnapshotAsync(file)if (snapshot) { univerAPI.createBase(snapshot)}Importing a snapshot does not require collaboration, but the file must still be converted on the server. If users need to return to the Base later, store the result of base.save() as described in saving and restoring snapshots.
Export a file
To export the current Base as Excel:
import { ExchangeFormat } from '@univerjs-pro/exchange-client'const base = univerAPI.getActiveBase()if (!base) throw new Error('Open a Base first')const file = await univerAPI.exportBaseBySnapshotAsync(base.save(), ExchangeFormat.XLSX)if (file) { univerAPI.downloadFile(file, 'database', ExchangeFormat.XLSX)}CSV and TSV can represent only one table, so pass tableId explicitly when exporting a Base that has more than one table:
const table = base.getTables()[0]if (!table) throw new Error('Create a table first')const file = await univerAPI.exportBaseBySnapshotAsync( base.save(), ExchangeFormat.CSV, table.getId(),)if (file) { univerAPI.downloadFile(file, table.getName(), ExchangeFormat.CSV)}For TSV, replace ExchangeFormat.CSV with ExchangeFormat.TSV. File export is intended for exchanging table data. For a full backup, save the Base snapshot; CSV does not preserve views, permissions, or any other resource.
Import a collaborative document
Configure Bases collaboration first, then import the document and load the ID returned by the server:
import '@univerjs-pro/collaboration-client/facade'const unitId = await univerAPI.importBaseToUnitIdAsync(file)if (unitId) { await univerAPI.getCollaboration().loadBaseAsync(unitId)}To export a server document, call exportBaseByUnitIdAsync(unitId, format, tableId?). Wait for pending browser edits to finish syncing before exporting.
Verify field and formula conversion with real application files. These methods can return undefined and can throw request or conversion errors, so handle both cases in the UI.
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 the application still requires the previous import/export 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 complete source code is available there for online reading or download.
How is this guide?