Export visible columns and any TanStack Table row model to a correctly escaped CSV file.
npm install tanstack-table-export-to-csvimport { exportToCsv } from "tanstack-table-export-to-csv";
function ExportButton({ table }) {
const exportRows = () => {
const headers = table
.getHeaderGroups()
.flatMap((group) => group.headers);
exportToCsv("people", headers, table.getFilteredRowModel().rows);
};
return <button onClick={exportRows}>Export CSV</button>;
}The filename receives a .csv extension automatically. Choose the row model that matches the export:
table.getCoreRowModel().rowsexports all rows.table.getFilteredRowModel().rowsexports filtered rows.table.getRowModel().rowsexports the current page.table.getSelectedRowModel().rowsexports selected rows.
import { getCsvBlob } from "tanstack-table-export-to-csv";
const blob = getCsvBlob(headers, rows);Both exportToCsv and getCsvBlob accept the same optional fourth/third argument:
exportToCsv("people", headers, rows, {
delimiter: ";",
includeBom: true,
preventFormulaInjection: true,
formatCell: (value, cell) =>
cell.column.id === "createdAt"
? new Date(String(value)).toLocaleDateString()
: value,
});delimitersupports comma, semicolon, or tab. The default is comma.includeBomhelps Excel detect UTF-8 text such as Cyrillic correctly.preventFormulaInjectionprefixes dangerous string values with an apostrophe. Enable it when exported data can contain untrusted user input.formatCellformats dates, currencies, arrays, objects, or any other cell value.
The default export remains available for backwards compatibility:
import exportToCsv from "tanstack-table-export-to-csv";exportToCsv(fileName, headers, rows, options?)creates and downloads a CSV file.getCsvBlob(headers, rows, options?)returns aBlobwith MIME typetext/csv;charset=utf-8.getHeaderNames(headers)returns the visible leaf-column names.
Fields containing commas, quotes, or line breaks are escaped according to standard CSV rules. Hidden columns, grouped headers, and placeholder headers are omitted.