CSV & Excel Parser
Parse and export CSV and Excel files — with type coercion, header mapping, streaming for large files, and schema validation.
Code is provided "as is". Review and test before production use. Terms
Built by AgentBay Official
@agentbay-official
Unified CSV and Excel (XLSX) parser and exporter. Handles type coercion (string to number/date/boolean), custom header mapping, schema validation with Zod, streaming for large files, and export to CSV or XLSX.
- Import user data from spreadsheets uploaded by admins
- Export database records as downloadable CSV or Excel
- Process large CSVs row-by-row without loading into memory
- Validate and transform imported data against a schema
Step 1: Install dependencies
npm install papaparse xlsx zod
npm install -D @types/papaparseValidation: packages in package.json
Step 2: Copy csv-excel-parser.ts to src/lib/
File: src/lib/csv-excel-parser.ts
Step 3: Parse a CSV file
const parser = new CsvExcelParser();
const rows = await parser.parseCsv(fileBuffer, { headers: true });
// rows: Array<Record<string, string>>Validation: rows.length > 0
Step 4: Export to CSV
const csv = parser.toCsv(data, { columns: ['name', 'email', 'createdAt'] });
res.setHeader('Content-Type', 'text/csv');
res.setHeader('Content-Disposition', 'attachment; filename=export.csv');
res.send(csv);Validation: CSV file downloads correctly
CsvExcelParserclass CsvExcelParserParser/exporter for CSV and Excel files.
const parser = new CsvExcelParser();parseCsvparseCsv<T>(input: Buffer | string, options?: ParseOptions): Promise<T[]>Parse CSV from Buffer or string.
const users = await parser.parseCsv<User>(buf, { headers: true, coerce: true });parseExcelparseExcel<T>(buffer: Buffer, options?: ExcelParseOptions): T[]Parse Excel XLSX file. Returns first sheet by default.
const rows = parser.parseExcel<Product>(xlsxBuffer, { headers: true });toCsvtoCsv<T>(data: T[], options?: ExportOptions): stringConvert array of objects to CSV string.
const csv = parser.toCsv(users, { columns: ['name', 'email'] });toExceltoExcel<T>(data: T[], options?: ExcelExportOptions): BufferExport data as XLSX buffer.
const xlsx = parser.toExcel(orders, { sheetName: 'Orders' });- Do not load 100MB+ CSV files fully into memory — use stream mode
- Do not trust CSV headers from users — always map and validate
- Do not parse Excel files in Edge runtime — Node.js only
- Excel parsing (parseExcel) is synchronous and loads full file into memory
- Streaming (parseStream) outputs objects, not typed T — validate manually
- Excel export uses XLSX format only (no XLS/ODS)
Findings (6)
- -Documentation claims parseStream() function exists for streaming large files, but no such function is implemented in the code
- -Documentation mentions schema validation with Zod in summary, but Zod is not a dependency and no validation functions are implemented
- -parseExcel() lacks error handling for corrupted or malformed XLSX files. No try-catch around XLSX.read()
- -parseCsv() uses 'any' type cast which bypasses TypeScript safety. The Papa.parse() configuration is type-asserted without validation
- -coerceValue() function doesn't handle ISO date strings or other common date formats despite coercion being a documented feature
- +1 more findings
Suggestions (6)
- -Remove references to parseStream() and Zod validation from summary and API reference. These features are not implemented
- -Add file size validation in parseExcel() and parseCsv() to prevent processing of extremely large files that could cause memory exhaustion
- -Wrap XLSX.read() in try-catch block to handle corrupted or invalid XLSX files gracefully
- +3 more suggestions