Exports: xlsx, CSV
Users often want a list as a spreadsheet: “give me the stations in
Excel”. rind can answer any list, or any single object, as an xlsx
workbook or a CSV file, with the same filters, search and ordering as
the JSON list.
In Django REST framework, this is the job of a renderer such as
drf-excel’s XLSXRenderer or djangorestframework-csv’s
CSVRenderer, chosen with ?format= or the Accept header. rind works
the same way from the client’s side. On the server’s side, an export
serializer says which columns a person sees, separately from the JSON
fields.
Turning exports on
The formats are plugins of the application. The sample installs both:
#![allow(unused)]
fn main() {
// sample/src/lib.rs
.with(
rind::export::Xlsx::new()
.style("count", rind::export::Style::new().number_format("0").bold()),
)
.with(rind::api::export::Csv::excel()) // `;` and a byte-order mark: Excel in French
}
Xlsx needs the xlsx feature of the rind crate. Csv is always
there. Csv::new() writes the standard CSV, with commas.
Csv::excel() writes ; and a UTF-8 byte-order mark, which is what
Excel opens correctly in a French locale.
Once a format is installed, every resource can be exported. Nothing else is needed for a first export.
Asking for an export
A client asks for a format with ?format=:
GET /stations?format=xlsx&ordering=code&fields=code,name,region_details
GET /stations/1?format=csv
Or with the Accept header, when there is no ?format=:
GET /stations
Accept: application/xlsx
Accept may name application/xlsx (what a real Django application’s
front end sent), the registered
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, or
text/csv.
The answer is a file to download:
Content-Disposition: attachment; filename="stations-2026-11-16.xlsx"
A list’s file is named after the resource and the day. A single object’s
file is named after the resource and the id (stations-1.csv).
Which rows are exported
The rows are the list’s: its filters, its search, its ordering, and only
the fields the request selects (fields, omit, include). The export
of /stations?status=closed holds the closed stations, as the JSON list
would.
Every row is exported, not only the first page, up to the resource’s
largest page size (50,000 by default; see
Pages). A list longer than that is a 400 that says
so, unless the request gives a page_size: then the export holds that
page only.
The cache does not keep an export, and ETags leaves it
alone.
Choosing the columns: an export serializer
Without an export serializer, an export has one column per field of the answer, in the output’s order, labelled by the field’s name. That is enough for a quick look, but a person reading a spreadsheet wants labels in their language, widths, a date as a date, and sometimes one column per value (a column per service).
An export serializer says this. It is a struct that derives Export,
written from the output it exports (of = StationOut). Each field is
one column, or a group of columns:
#![allow(unused)]
fn main() {
// sample/crates/network/network-api/src/stations/exports.rs
use rind::Export;
use rind::api::export::{Cell, Column, Columns};
use super::serializers::StationOut;
#[derive(Export)]
#[export(of = StationOut, sheet = "Stations", name = "stations")]
pub struct StationXlsx {
#[column(label = "Code", width = 8)]
pub code: Column,
#[column(label = "Nom", width = 24)]
pub name: Column,
#[column(label = "Ville")]
pub city: Column,
/// Through a nested output: a column only when it is asked for.
#[column(label = "Région", from = region_details.name)]
pub region: Column,
#[column(label = "Ouverte le")]
pub opened: Column,
#[column(label = "Quais", style = "count")]
pub platforms: Column,
#[column(label = "Photo")]
pub photo: Column,
/// A function of the typed output.
#[column(label = "Position", with = position, needs(position))]
pub position: Column,
/// A column per service, made from the rows.
#[columns(with = services, needs(services))]
pub services: Columns,
}
fn position(station: &StationOut) -> Option<String> {
let point = station.position.flatten()?;
Some(format!("{:.4}, {:.4}", point.lat, point.lng))
}
fn services(station: &StationOut) -> Vec<(String, String, Cell)> {
station
.services
.iter()
.flatten()
.map(|s| (format!("service_{s}"), s.to_uppercase(), Cell::from("x")))
.collect()
}
}
The resource lists its export serializers:
#![allow(unused)]
fn main() {
// sample/crates/network/network-api/src/stations/views.rs
#[resource(model = Station, /* … */ exports(StationXlsx, StationShortXlsx))]
pub struct StationResource;
}
The same export serializer is used for every format: xlsx and CSV get the same columns.
Where a column’s value comes from
There are three kinds of columns:
- A field of the answer. By default, a column reads the field of
the same name (
codereadscode).from = a.breads another field, or a field inside a nested output:from = region_details.namereads the region’s name. - A function.
with = fcallsf(&StationOut)on each row. It returns any value that converts into a cell (see below). - A group of columns.
#[columns(with = f)]makes columns from the rows.freturns(key, label, value)triples. There is one column per key, in the order the keys first appear. This is how you get a column per service, or a price per period.
The names in from and needs are checked against the output when the
application compiles: a misspelt field is a compile error.
A column is left out when its field is not selected
The client still chooses the fields with ?fields=. A column whose
field the request does not select is left out of the sheet:
- A
fromcolumn, or a column withoutfrom, needs the first field of its path. The “Région” column is there only when the request selectsregion_details. - A
withcolumn, or a group of columns, needs the fields named inneeds(..). Withoutneeds, it is always there.
Cells
A value read from the answer becomes a typed cell:
- a number is a number;
- a date (
2026-11-16) is a date, and a date and time is a date and time; - a file (
{url, name}) is a link; - a list of values is one text,
a, b; nullis an empty cell.
A with function may return a text, a number, a boolean, a date, an
Option of one, Cell::link(url, text), or Cell::Formula(..) (a
formula without its =).
Several exports for one resource
A resource can list several export serializers. The first one is the
default. A client chooses another by its name:
#![allow(unused)]
fn main() {
// sample/crates/network/network-api/src/stations/exports.rs
/// A short one: `?export=short`.
#[derive(Export)]
#[export(of = StationOut, sheet = "Stations", name = "short")]
pub struct StationShortXlsx {
#[column(label = "Code")]
pub code: Column,
#[column(label = "Nom")]
pub name: Column,
}
}
GET /stations?format=xlsx&export=short
An unknown export name is a 400 (No export named …).
Styles
The xlsx format has named styles. A column names its cells’ style with
style = "…" and its header’s style with header_style = "…". The
styles themselves are defined once, on the Xlsx plugin:
#![allow(unused)]
fn main() {
use rind::export::Style;
rind::export::Xlsx::new()
.style("count", Style::new().number_format("0").bold())
.style("header", Style::new().bold().background(0x02B5B1).color(0xFFFFFF))
.date_formats("dd/mm/yyyy", "dd/mm/yyyy hh:mm")
}
A header without its own style uses the style named header. By
default, it is bold, on a light grey background, with a border.
Defining header again, as above, changes every header.
date_formats sets how dates and date-times are shown. The default is
yyyy-mm-dd and yyyy-mm-dd hh:mm:ss.
In every workbook, the header row is frozen and has filters. The workbook is written row by row, so a large export does not hold the whole sheet twice in memory.
CSV has no styles: the style names are ignored there.
A format of your own
A format is a type that implements rind::api::export::Format. It
gives its name (the value of ?format=), the media types Accept may
name, its Content-Type, its file extension, and a write function
that turns a Sheet (columns and rows of cells) into bytes. A plugin
installs it with Setup::format.
The OpenAPI document then gives every read operation the format and
export parameters, and lists the formats’ media types among its
answers.
Reference
#[export(..)]
| Key | Meaning |
|---|---|
of = Out | The output it exports. Required. |
name = "…" | Its name for ?export=. By default, the struct’s name. |
sheet = "…" | The sheet’s name. By default, the struct’s name. Excel keeps 31 characters, and replaces []:*?/\. |
style = "…" | The default style of its columns. |
header_style = "…" | The default style of its headers. |
#[column(..)] and #[columns(..)]
A field of an export serializer is a Column with #[column(..)], or
Columns with #[columns(..)].
| Key | Meaning |
|---|---|
label = "…" | The header. By default, the field’s name. |
key = "…" | The column’s key. By default, the field’s name. |
from = a.b | The answer’s value at that path, by the output’s names. By default, the field of the same name. Not with with. |
with = f | f(&Out), any value Into<Cell>. On #[columns]: required, and f returns Vec<(key, label, Cell)>. |
needs(a, b) | The column is left out when one of these fields is not selected. A from column needs its first field anyway. |
width = N | The width, in characters. |
style = "…", header_style = "…" | The styles of its cells and of its header. |
Style
Style::new(), then any of: .bold(), .italic(), .size(points),
.color(0xRRGGBB), .background(0xRRGGBB), .align(Align::…),
.wrap(), .border(), .number_format("0.00").
Formats
| Format | ?format= | Accept | Feature |
|---|---|---|---|
rind::export::Xlsx | xlsx | application/xlsx, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet | xlsx |
rind::api::export::Csv | csv | text/csv | always |
Query parameters
| Parameter | Meaning |
|---|---|
format | The format’s name. |
export | The export serializer’s name; the first by default. |
Neither name may be used by a filter or a typed parameter.