# How to Export Query Results from DBX to XLSX, CSV, and JSON

> Easily export DBX query results to XLSX, CSV, and JSON. Learn how to serialize your data for desktop UI and CLI access.

- Repository: [skyler/dbx](https://github.com/t8y2/dbx)
- Tags: how-to-guide
- Published: 2026-07-05

---

**DBX exports query results through Tauri backend commands that serialize data to CSV, XLSX, JSON, or Markdown formats, accessible via both the desktop UI and CLI.**

DBX provides a Tauri-based desktop architecture that cleanly separates database query execution from result formatting. When you need to export query results from DBX, the system marshals raw column and row data through Rust commands to generate properly formatted files. This article examines the exact source files, command signatures, and implementation patterns that enable data export across multiple formats.

## The DBX Export Architecture

DBX implements a two-layer export system that separates formatting logic from file I/O operations. When a query executes via `dbx_core::database::execute_query`, it returns a `QueryResult` struct containing `columns: Vec<String>` and `rows: Vec<Vec<Value>>`.

The export flow follows this path:

1. **Formatting layer** – Pure Rust functions in `dbx_core` convert the `QueryResult` into format-specific strings or binary data
2. **I/O layer** – Tauri commands in `src-tauri/src/commands/` receive the formatted data and write it to the user-specified file path with UTF-8 BOM encoding
3. **Interface layer** – TypeScript wrappers in [`apps/desktop/src/lib/backend/tauri.ts`](https://github.com/t8y2/dbx/blob/main/apps/desktop/src/lib/backend/tauri.ts) expose these capabilities to the frontend, while [`packages/cli/src/cli.ts`](https://github.com/t8y2/dbx/blob/main/packages/cli/src/cli.ts) handles CLI invocations

This architecture ensures that the same core logic serves both the desktop application and command-line interface.

## Supported Export Formats and Implementation Paths

DBX supports four export formats through dedicated Tauri commands. Each format has a specific implementation file and command name used for invocation.

### CSV Export

**Command:** `export_query_result_csv`

**Implementation:** [`src-tauri/src/commands/csv_export.rs`](https://github.com/t8y2/dbx/blob/main/src-tauri/src/commands/csv_export.rs) contains the `format_query_result_csv` function, which handles comma-separated value generation.

**Frontend wrapper:** [`apps/desktop/src/lib/backend/tauri.ts`](https://github.com/t8y2/dbx/blob/main/apps/desktop/src/lib/backend/tauri.ts) exports `exportQueryResultCsv` for UI consumption.

### XLSX Export

**Command:** `export_query_result_xlsx` (single sheet) or `export_query_results_xlsx` (multiple sheets)

**Implementation:** [`src-tauri/src/commands/xlsx_export.rs`](https://github.com/t8y2/dbx/blob/main/src-tauri/src/commands/xlsx_export.rs) implements the Excel export logic, delegating to `dbx_core::xlsx_export` for workbook generation.

### JSON and Markdown Export

**Commands:** `export_query_result_json` and `export_query_result_markdown`

**Implementation:** Both formats reside in [`src-tauri/src/commands/text_export.rs`](https://github.com/t8y2/dbx/blob/main/src-tauri/src/commands/text_export.rs), which provides `format_json` and `format_markdown` functions for serialization.

## Exporting from the Command Line

The DBX CLI parses the `--format` flag in [`packages/cli/src/cli.ts`](https://github.com/t8y2/dbx/blob/main/packages/cli/src/cli.ts) and routes output to the appropriate backend command.

```bash

# CSV (default table view → CSV)

dbx query local "SELECT * FROM users" --format csv > users.csv

# JSON

dbx query local "SELECT * FROM users" --format json > users.json

# XLSX (requires explicit output path)

dbx query local "SELECT * FROM users" --format xlsx --output users.xlsx

# Markdown (ideal for documentation)

dbx query local "SELECT * FROM users" --format markdown > users.md

```

For table formatting in terminal output, the CLI also references helper functions in [`packages/cli/src/cli-format.ts`](https://github.com/t8y2/dbx/blob/main/packages/cli/src/cli-format.ts) (such as `csvTable`) to build display tables before file export.

## Programmatic Export in TypeScript/JavaScript

Frontend applications can trigger exports by importing wrappers from the Tauri backend module. These functions handle the `invoke` calls to Rust commands.

```typescript
import {
  exportQueryResultCsv,
  exportQueryResultXlsx,
  exportQueryResultJson,
  exportQueryResultMarkdown,
} from '@/lib/backend/tauri';

// Data structure from query execution
const columns = ['id', 'name', 'email'];
const rows = [
  [1, 'Ada', 'ada@example.com'],
  [2, 'Bob', 'bob@example.com'],
];

// CSV export
await exportQueryResultCsv('/tmp/users.csv', columns, rows);

// XLSX export (specifies sheet name)
await exportQueryResultXlsx('/tmp/users.xlsx', 'Users', columns, rows);

// JSON export
await exportQueryResultJson('/tmp/users.json', columns, rows);

// Markdown export
await exportQueryResultMarkdown('/tmp/users.md', columns, rows);

```

The functions in [`apps/desktop/src/lib/backend/tauri.ts`](https://github.com/t8y2/dbx/blob/main/apps/desktop/src/lib/backend/tauri.ts) serve as thin abstraction layers that pass arguments to the corresponding Tauri commands with proper type safety.

## Exporting Selected Data Rows

When users need to export only a subset of data from the grid interface, DBX provides the `useDataGridExport` composable in [`apps/desktop/src/composables/useDataGridExport.ts`](https://github.com/t8y2/dbx/blob/main/apps/desktop/src/composables/useDataGridExport.ts).

```typescript
const { exportQueryResultViaBackend } = useDataGridExport();

// Export only the currently selected rows as CSV
await exportQueryResultViaBackend('csv');

// Export selection as XLSX
await exportQueryResultViaBackend('xlsx');

```

This composable packages the selected row indices, retrieves the corresponding data from the grid state, and calls the appropriate backend export function with the filtered dataset.

## Summary

- **DBX** uses a Tauri-based backend to handle query result exports, separating formatting logic (`dbx_core`) from file I/O operations (Tauri commands).
- **Four formats** are supported: CSV via `export_query_result_csv`, XLSX via `export_query_result_xlsx`, JSON via `export_query_result_json`, and Markdown via `export_query_result_markdown`.
- **CLI access** uses the `--format` flag parsed in [`packages/cli/src/cli.ts`](https://github.com/t8y2/dbx/blob/main/packages/cli/src/cli.ts), supporting redirection to files or explicit `--output` paths for binary formats like XLSX.
- **Frontend integration** occurs through TypeScript wrappers in [`apps/desktop/src/lib/backend/tauri.ts`](https://github.com/t8y2/dbx/blob/main/apps/desktop/src/lib/backend/tauri.ts), which invoke the Rust commands via Tauri's IPC mechanism.
- **Selective export** is available through the `useDataGridExport` composable, allowing users to export only highlighted rows from the data grid.

## Frequently Asked Questions

### What file formats does DBX support for query export?

DBX supports **CSV**, **XLSX** (Excel), **JSON**, and **Markdown** formats. Each format has a dedicated Rust implementation file in `src-tauri/src/commands/` and a corresponding Tauri command that handles the serialization and file writing operations.

### How do I export only selected rows instead of the full result set?

Use the `useDataGridExport` composable from [`apps/desktop/src/composables/useDataGridExport.ts`](https://github.com/t8y2/dbx/blob/main/apps/desktop/src/composables/useDataGridExport.ts). This utility captures the current selection from the data grid and passes only those rows to the backend export commands, rather than serializing the entire `QueryResult` returned by the database engine.

### Where is the CSV export logic implemented in the source code?

The CSV formatting logic resides in [`src-tauri/src/commands/csv_export.rs`](https://github.com/t8y2/dbx/blob/main/src-tauri/src/commands/csv_export.rs) within the `format_query_result_csv` function. The frontend binding that calls this logic is located in [`apps/desktop/src/lib/backend/tauri.ts`](https://github.com/t8y2/dbx/blob/main/apps/desktop/src/lib/backend/tauri.ts), which exports the `exportQueryResultCsv` wrapper function for UI components.

### Can I use DBX export functionality in automated scripts?

Yes. The DBX CLI supports automated exports through the `--format` flag (accepting `csv`, `json`, `xlsx`, or `markdown`) and the `--output` parameter for specifying destination paths. This is parsed in [`packages/cli/src/cli.ts`](https://github.com/t8y2/dbx/blob/main/packages/cli/src/cli.ts) and executes the same backend commands used by the desktop application, making it suitable for CI/CD pipelines and batch processing.