# Import and export

- Human documentation: [https://docs.univer.ai/guides/docs/features/import-export](https://docs.univer.ai/guides/docs/features/import-export)

- Agent Markdown: [https://docs.univer.ai/guides/docs/features/import-export.md](https://docs.univer.ai/guides/docs/features/import-export.md)

- Requested language: `en-US`

- Content language: `en-US`

- Documentation version: `1.0.0-rc.0`

- Source: [docs/features/import-export.mdx](https://github.com/dream-num/documentation/blob/dev/content/guides/docs/features/import-export.mdx)

---

The APIs below need a conversion backend. Follow [file exchange integration](https://docs.univer.ai/server/import-export.md) 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.

#### Package metadata

```json
{
  "preset": [
    {
      "client": "@univerjs/preset-docs-advanced",
      "locale": "@univerjs/preset-docs-advanced/locales/en-US",
      "style": "@univerjs/preset-docs-advanced/lib/index.css"
    }
  ],
  "plugins": [
    {
      "client": "@univerjs-pro/exchange-client",
      "locale": "@univerjs-pro/exchange-client/locale/en-US",
      "style": "@univerjs-pro/exchange-client/lib/index.css",
      "facade": "@univerjs-pro/exchange-client/facade"
    },
    {
      "client": "@univerjs-pro/docs-exchange-client",
      "facade": "@univerjs-pro/docs-exchange-client/facade",
      "locale": "@univerjs-pro/docs-exchange-client/locale/en-US"
    }
  ],
  "license": true,
  "server": true
}
```

## 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

> [!INFO: 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.

```typescript
import '@univerjs-pro/exchange-client/facade'
import '@univerjs-pro/docs-exchange-client/facade'
```

### Importing `.docx`

#### Import `.docx` file and get `unitId`

> [!WARNING: 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.

```typescript
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`

> [!NOTE]
> Snapshot import does not require collaboration. Use 
> 
> `unitId`
> 
>  import for collaborative documents.

Using `univerAPI.importDocToSnapshotAsync` to import a `.docx` file will return the document data in the `IDocumentData` format.

```typescript
// Accept a File object
const 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 snapshot
if (snapshot) univerAPI.createDocument(snapshot)
```

### Exporting `.docx`

#### Export `.docx` file using `unitId`

> [!WARNING: 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.

```typescript
const fDocument = univerAPI.getActiveDocument()
const unitId = fDocument.getId()
const file = await univerAPI.exportDocByUnitIdAsync(unitId)

// Download the file through the Facade API
if (file) univerAPI.downloadFile(file, 'univer', 'docx')
```

#### Export `.docx` file using `IDocumentData`

> [!NOTE]
> Snapshot export does not require collaboration and is suitable for local workflows.

Using `univerAPI.exportDocBySnapshotAsync` with `IDocumentData` will return a `File` object.

```typescript
const fDocument = univerAPI.getActiveDocument()
const snapshot = fDocument.save()
const file = await univerAPI.exportDocBySnapshotAsync(snapshot)

// Download the file through the Facade API
if (file) univerAPI.downloadFile(file, 'univer', 'docx')
```
