CSV, JSON and import
exportCsv and exportJson save what people see, and importFile reads .xlsx and CSV files back in, checking every value like a paste.
Live demoCSV, JSON & import
Save what people see as CSV or JSON, and read Excel or CSV files back in.
Code for this demo
import { createGrid, type ColumnDef } from '@freegridjs/core';
import { makeRows } from '../data';
import { button, separator, text, tr, type DemoContext } from '../ui';
export default function mount(ctx: DemoContext) {
const { lang } = ctx;
const h = (en: string, ko: string) => tr(lang, { en, ko });
const columns: ColumnDef[] = [
{ field: 'id', header: 'ID', type: 'number', width: 80 },
{ field: 'name', header: h('Name', '이름'), width: 160, editable: true, align: 'center' },
{ field: 'country', header: h('Country', '국가'), width: 140, filter: 'set', editable: true },
{ header: h('Revenue', '매출'), children: [
{ field: 'q1', header: h('Q1', '1분기'), type: 'number', format: '#,##0', width: 110, editable: true },
{ field: 'q2', header: h('Q2', '2분기'), type: 'number', format: '#,##0', width: 110, editable: true },
] },
{ field: 'growth', header: h('Growth', '성장률'), type: 'number', format: '0.0%', width: 100 },
{ field: 'joined', header: h('Joined', '가입일'), type: 'date', width: 120, editable: true },
{ field: 'active', header: h('Active', '활성'), type: 'boolean', width: 80, editable: true },
];
const grid = createGrid(ctx.host, {
columns,
data: makeRows(300),
locale: lang,
theme: 'classic',
rowSelection: 'multiple',
filterRow: true,
});
grid.on('filterChange', e => ctx.log(`filterChange → ${Object.keys(e.filters).join(', ') || h('none', '없음')}`));
text(ctx, h('Filter with the boxes under the header, then export only what you see. Import an .xlsx or CSV file to add rows.',
'헤더 아래 칸으로 걸러 낸 뒤 보이는 행만 내보내 보세요. .xlsx나 CSV 파일을 가져오면 행이 추가됩니다.'));
separator(ctx);
button(ctx, h('Export CSV', 'CSV로 내보내기'), () => grid.exportCsv(h('freegrid-demo.csv', 'freegrid-데모.csv')));
button(ctx, h('Export JSON', 'JSON으로 내보내기'), () => grid.exportJson(h('freegrid-demo.json', 'freegrid-데모.json')));
button(ctx, h('JSON as shown, by header', '화면 글자 그대로, 헤더를 키로'), () => {
const json = grid.getJson({ keys: 'header', formatted: true, rows: 'filtered', space: 0 });
ctx.log(json.slice(0, 160) + '…');
});
button(ctx, h('Checked rows as CSV', '체크한 행만 CSV로'), () => grid.exportCsv('selected.csv', { rows: 'selected' }));
separator(ctx);
// Import: a hidden file input behind a normal button
const input = document.createElement('input');
input.type = 'file';
input.accept = '.xlsx,.csv,.tsv,.txt';
input.hidden = true;
input.addEventListener('change', async () => {
const file = input.files?.[0];
input.value = '';
if (!file) return;
try {
const res = await grid.importFile(file, { mode: 'append' });
ctx.log(h(`imported ${res.rows.length} rows (${res.skipped} cells skipped) — Ctrl+Z undoes it`,
`${res.rows.length}행을 가져왔습니다 (건너뛴 셀 ${res.skipped}개) — Ctrl+Z로 되돌릴 수 있습니다`));
grid.scrollToRow(grid.getDisplayedRowCount() - 1);
} catch (e) {
ctx.log(`import failed: ${(e as Error).message}`);
}
});
ctx.controls.append(input);
button(ctx, h('Import Excel / CSV…', '엑셀·CSV 가져오기…'), () => input.click());
return grid;
}exportCsv() saves the rows as CSV with a byte order mark, so Excel opens Korean and other non-Latin text correctly. Text that looks like a formula is prefixed so a spreadsheet never runs it. exportJson() saves the rows as an array of objects keyed by column id, or by header with keys: 'header'. Numbers, booleans and blanks keep their types, computed columns give their results, and formatted: true saves the text as shown.
Both export what is on screen by default: the current sort and filters, rows in the order the grid lists them (group by group, parents before children, collapsed rows included), and the visible columns. rows: 'all' takes every row in source order and rows: 'selected' the checked rows that pass the filters, in that same order; columns picks columns by id, hidden ones included. getCsv() and getJson() return the text instead of downloading it, and separator changes the CSV separator, for example to ;.
// Download what people see (current sort and filters)
grid.exportCsv('orders.csv');
grid.exportCsv('orders.csv', { separator: ';' });
grid.exportJson('orders.json');
// Pick columns and rows
grid.exportCsv('picked.csv', { columns: ['name', 'total'], rows: 'selected' });
grid.exportJson('orders.json', { keys: 'header', formatted: true });
const text = grid.getJson({ columns: ['id', 'amount'], space: 0 }); // [{"id":1,"amount":32000},…]
// Read a file chosen by the user (.xlsx or CSV)
input.addEventListener('change', async () => {
const { rows, skipped } = await grid.importFile(input.files[0]); // { mode: 'replace' } to swap
});
// Without a grid
import { parseCsv, readXlsx } from '@freegridjs/core';
const lines = parseCsv('name;total
Kim;1200'); // the separator is detected
const sheets = readXlsx(new Uint8Array(await file.arrayBuffer()));Import Excel and CSV files
importFile() reads an .xlsx or CSV file. The first row is matched to your columns by header or field name, and every value is checked like a paste, so a number column only takes numbers and a date column takes dates such as 2026-03-05 or 2026.3.5. Imported rows are added at the end and can be undone with Ctrl+Z, or mode: 'replace' swaps the data; sheet picks a worksheet. The result says how many rows came in and how many cells were skipped. Without a grid, readXlsx() and parseCsv() read the same files into plain arrays.
To save real Excel files with formats and colours, several grids in one workbook, or PDF files, see Excel and PDF export. To print, see Printing.
Questions about this page
Can FreeGrid export to CSV and JSON?
Yes: exportCsv writes UTF-8 CSV with a byte order mark that opens in Excel, and exportJson writes an array of row objects; both keep the current sort and filters.
Can users import Excel files into the grid?
Yes. importFile reads .xlsx and CSV files, matches columns by header or field, and checks values the same way as pasting.