typed-xlsx
Workbook

Web Frameworks

Return an Excel file from a Next.js route handler or a NestJS controller, from rows in memory or streamed in pages.

Both builders end in a value a framework can send. The buffered builder gives a Blob or bytes; the streaming builder gives a Web ReadableStream or writes to a Node response. Express, Fastify and Hono are covered in Output Targets.

Next.js route handler

In the App Router, a route handler returns a Web Response, so no Next.js import is needed. Run it on the Node.js runtime: the streaming builder spools rows to temporary files, and the buffered builder compresses with Node's zlib.

Rows already in memory:

// app/orders/export/route.ts
import { 
createExcelSchema
,
createWorkbook
} from "typed-xlsx";
type
Order
= {
id
: string;
customer
: string;
total
: number };
declare function
loadOrders
():
Promise
<
Order
[]>;
const
orders
=
createExcelSchema
<
Order
>()
.
column
("id", {
header
: "Order",
accessor
: "id" })
.
column
("customer", {
header
: "Customer",
accessor
: "customer" })
.
column
("total", {
header
: "Total",
accessor
: "total",
format
: "#,##0.00" })
.
build
();
export const
runtime
= "nodejs";
export async function
GET
() {
const
workbook
=
createWorkbook
();
workbook
.
sheet
("Orders").
table
("orders", {
schema
:
orders
,
rows
: await
loadOrders
() });
return new
Response
(await
workbook
.
toBlob
(), {
headers
: { "Content-Disposition": 'attachment; filename="orders.xlsx"' },
}); }

The Blob carries the xlsx content type, so the response gets it too.

Rows read in pages, streamed out:

// app/orders/export/route.ts
import { 
createExcelSchema
,
createWorkbookStream
} from "typed-xlsx";
type
Order
= {
id
: string;
customer
: string;
total
: number };
declare function
orderPages
(
signal
:
AbortSignal
):
AsyncIterable
<
Order
[]>;
const
orders
=
createExcelSchema
<
Order
>()
.
column
("id", {
header
: "Order",
accessor
: "id" })
.
column
("customer", {
header
: "Customer",
accessor
: "customer" })
.
column
("total", {
header
: "Total",
accessor
: "total",
format
: "#,##0.00" })
.
build
();
export const
runtime
= "nodejs";
export async function
GET
(
request
:
Request
) {
// Removes the temp files if a page fails before the response starts. await using
workbook
=
createWorkbookStream
();
const
table
= await
workbook
.
sheet
("Orders").
table
("orders", {
schema
:
orders
});
await
table
.
commitPages
(
orderPages
(
request
.
signal
), {
signal
:
request
.
signal
});
return new
Response
(
workbook
.
toReadableStream
(), {
headers
: {
"Content-Type": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "Content-Disposition": 'attachment; filename="orders.xlsx"', }, }); }

Returning the stream from inside the await using scope is safe: once the output has started, the workbook cleans up when the last byte is sent or the client disconnects. See Clean up when an export fails.

On the Edge runtime, pass createWorkbookStream({ tempStorage: "memory" }), or use the buffered builder's toBlob(), which compresses with CompressionStream.

NestJS controller

Rows already in memory: return a StreamableFile from the bytes.

import { 
Controller
,
Get
,
Header
,
Res
,
StreamableFile
} from "@nestjs/common";
import type {
ServerResponse
} from "node:http";
import {
createExcelSchema
,
createWorkbook
,
createWorkbookStream
} from "typed-xlsx";
type
Order
= {
id
: string;
customer
: string;
total
: number };
declare function
loadOrders
():
Promise
<
Order
[]>;
declare function
orderPages
():
AsyncIterable
<
Order
[]>;
const
XLSX
= "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
const
orders
=
createExcelSchema
<
Order
>()
.
column
("id", {
header
: "Order",
accessor
: "id" })
.
column
("customer", {
header
: "Customer",
accessor
: "customer" })
.
column
("total", {
header
: "Total",
accessor
: "total",
format
: "#,##0.00" })
.
build
();
@
Controller
("orders")
export class
OrdersController
{
@
Get
("export")
@
Header
("Content-Type",
XLSX
)
@
Header
("Content-Disposition", 'attachment; filename="orders.xlsx"')
async
export
():
Promise
<
StreamableFile
> {
const
workbook
=
createWorkbook
();
workbook
.
sheet
("Orders").
table
("orders", {
schema
:
orders
,
rows
: await
loadOrders
() });
return new
StreamableFile
(
workbook
.
toUint8Array
());
} // Streamed: write straight to the response. With @Res(), Nest leaves the response to you. @
Get
("export/stream")
async
exportStream
(@
Res
()
res
:
ServerResponse
):
Promise
<void> {
await using
workbook
=
createWorkbookStream
();
const
table
= await
workbook
.
sheet
("Orders").
table
("orders", {
schema
:
orders
});
await
table
.
commitPages
(
orderPages
());
res
.
setHeader
("Content-Type",
XLSX
);
res
.
setHeader
("Content-Disposition", 'attachment; filename="orders.xlsx"');
await
workbook
.
pipeToNode
(
res
);
} }

With the Express platform, res is Express's Response, which is a Node ServerResponse; with Fastify, use @Res() reply: FastifyReply and pipeToNode(reply.raw). The headers are set after the last commit, so an export that fails while reading rows can still answer with an error status.

Copyright © 2026 Cyprien Thao. Released under the MIT License.