Office file import and export
Use Exchange to import and export content between Office files and collaborative Units.
Office file conversion is handled by @univerjs-pro/exchange-node, not by AI SDK. At the file boundary, a CLI application calls importFile() and exportToFile(), then uses AI SDK and Collaboration SDK to operate on the Unit.
Keep the SDK boundary
import { exportToFile, importFile } from "@univerjs-pro/exchange-node";| Capability | Owner |
|---|---|
| Converting Office files to and from UnitData | Exchange |
| Headless runtime, content inspection, and Facade execution | AI SDK |
| Snapshots, changesets, revisions, and persistence | Collaboration SDK |
| Paths, format policy, business APIs, and error mapping | Business application |
Import a collaborative Unit
The business application reads an Office file through Exchange and passes the resulting UnitData to Collaboration Server, which creates the Unit:
Office file→ importFile()→ UnitData→ business Server API→ collaborative UnitThe application determines the initial revision, the business metadata, and the result that is returned. Once the Unit exists, both the CLI and the Web operate it through the Server.
Export the latest revision
For export, the CLI loads the latest confirmed revision through Collaboration Runtime, then passes the complete UnitData to Exchange:
collaborative Unit→ latest UnitData→ exportToFile()→ Office fileThe extension of the output file determines the format.
Expose the file boundary through Commander
AI SDK does not provide a preset command package for Office file import and export. The business application uses Commander to combine Exchange, Collaboration Runtime, and its own Server API:
import { Command } from "commander";interface IFileExchangeDependencies { importFile(path: string): Promise<unknown>; createUnit(unitData: unknown): Promise<unknown>; loadLatestUnitData(unitId: string): Promise<unknown>; exportFile(unitData: unknown, path: string): Promise<void>; writeResult(result: unknown): void;}function addFileExchangeCommands(program: Command, dependencies: IFileExchangeDependencies): void { program.addCommand( new Command("import").argument("<office-file>").action(async (sourcePath) => { const unitData = await dependencies.importFile(sourcePath); const created = await dependencies.createUnit(unitData); dependencies.writeResult(created); }), ); program.addCommand( new Command("export") .argument("<office-file>") .requiredOption("--unit <id>") .action(async (outputPath, { unit }) => { const unitData = await dependencies.loadLatestUnitData(unit); await dependencies.exportFile(unitData, outputPath); }), );}Every capability outside Commander is declared explicitly in IFileExchangeDependencies, so the commands rely on no hidden helpers. The application performs file conversion by calling Exchange's importFile() and exportToFile(), implements loadLatestUnitData() on Collaboration Runtime, and injects its own Server API and result output.
Supported formats
| Unit | Import | Export |
|---|---|---|
| Sheet | .xls, .xlsx, .xlsm, .csv, .tsv | .xlsx, .csv, .tsv |
| Doc | .doc, .docx | .docx |
| Slide | .ppt, .pptx, .pptm, .ppsx, .ppsm, .potx | .pptx |
The application should reject an extension that does not match the Unit type before it generates a file.
Use only a local CLI
An application can bypass the Collaboration Server by passing imported content directly to a local headless runtime and exporting an Office file. This approach suits one-off processing, but it does not provide shared revisions, live Web preview, History, or Worktree.
Command names, path arguments, and result presentation remain the responsibility of the business application. For the collaborative Unit path, see Document loading and content operations. Once the file boundary is in place, continue with Visual inspection, which allows an agent to check the rendered UnitData.
How is this guide?