Courses/CSV / Excel Export
Standalone Course~30 min4 challenges

CSV / Excel Export

Data export is the feature every business app eventually needs and nobody enjoys building. In Grit it's automatic: every resource you scaffold withgrit generate resource ships a streaming CSV and chunked XLSX export at /export — built for constant memory.

Any resourceStreaming exportFilequerywriteResourcemany rowsStreaming exportconstant memoryCSV filedownload
Constant memoryDownload
Every resource exports as a stream — constant memory even for millions of rows

Reference docs: DataTable & Export →

Generated for free

Every resource from grit generate gets /export with no extra code.

CSV & XLSX

Pick the format with ?format=csv or ?format=xlsx.

Constant memory

Streaming CSV and chunked XLSX export millions of rows without blowing up RAM.

Filters carry through

The same query params that filter the list filter the export.

The export endpoint

When you generate a resource, Grit injects an export route alongside the usual CRUD handlers. You choose the format with a query parameter.

exporting a Product resource
# CSV (default) — streamed row-by-row
GET /api/products/export?format=csv

# Excel workbook — written in chunks
GET /api/products/export?format=xlsx

# Filters from the list endpoint apply to the export too
GET /api/products/export?format=xlsx&status=active&sort=created_at
Because export reuses the list query, whatever the user is looking at on screen is exactly what lands in the file — same filters, same sort, same scope.

Why streaming matters

The naive approach — load every row into a slice, build the whole file in memory, then send it — falls over on large tables. A 1-million-row export can eat gigabytes of RAM and time out. Grit avoids this by writing as it reads.

Streaming (CSV): Rows are fetched in batches and written straight to the HTTP response as they arrive. Memory stays flat regardless of row count because no full copy of the data is ever held.
Chunked (XLSX): The XLSX format is a zipped XML package, so it can't be streamed as plainly as CSV. Grit writes rows in chunks using a streaming writer, keeping peak memory bounded even for very large workbooks.
FormatBest forMemory profile
CSVHuge dumps, piping into other toolsFlat (true stream)
XLSXHand-off to non-technical usersBounded (chunked)
For exports that take a while, dispatch the export as a background job and email a download link when it's ready — Grit's jobs + email batteries make that a few lines.

From the admin panel

The admin DataTable includes an export menu wired to these endpoints, so end users can download the current view as CSV or Excel with one click — no API knowledge needed.

Practice

1

Challenge: Generate and export

Run grit generate resource Product --fields "name:string,price:float,stock:int", seed a few rows, then hit /api/products/export?format=csv. Open the file — do the columns match your fields?

2

Challenge: Switch to Excel

Request the same data with format=xlsx. Does it open cleanly in Excel, Numbers, or Google Sheets? What's different from the CSV?

3

Challenge: Carry a filter through

Add a filter query param (e.g. ?status=active) to the export URL. Does the file contain only the filtered rows? Why is reusing the list query the right design?

4

Challenge: Reason about scale

You need to export 5 million rows. Which format do you choose and why? What would you change so the user isn't left waiting on an open HTTP connection?