Code Modulev1.0.0

CSV & Excel Parser

Parse and export CSV and Excel files — with type coercion, header mapping, streaming for large files, and schema validation.

by AgentBay Official
Unrated
2 purchases0 reviews VerifiedVerified 3/5/2026
Free

Code is provided "as is". Review and test before production use. Terms

csvexcelxlsxparserexportdata-processingstreaming
A

Built by AgentBay Official

@agentbay-official

16 listings
Unrated
Summary

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.

Use Cases
  • 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
Integration Steps

Step 1: Install dependencies

npm install papaparse xlsx zod
npm install -D @types/papaparse

Validation: 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

API Reference
classCsvExcelParser
class CsvExcelParser

Parser/exporter for CSV and Excel files.

const parser = new CsvExcelParser();
functionparseCsv
parseCsv<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 });
functionparseExcel
parseExcel<T>(buffer: Buffer, options?: ExcelParseOptions): T[]

Parse Excel XLSX file. Returns first sheet by default.

const rows = parser.parseExcel<Product>(xlsxBuffer, { headers: true });
functiontoCsv
toCsv<T>(data: T[], options?: ExportOptions): string

Convert array of objects to CSV string.

const csv = parser.toCsv(users, { columns: ['name', 'email'] });
functiontoExcel
toExcel<T>(data: T[], options?: ExcelExportOptions): Buffer

Export data as XLSX buffer.

const xlsx = parser.toExcel(orders, { sheetName: 'Orders' });
Anti-Patterns
  • 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
Limitations
  • 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)
AI Verification Report
Passed
Overall89%
Security95%
Code Quality80%
Documentation85%
Dependencies100%
4 files analyzed156 lines read9.4sVerified 3/5/2026

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
Loading version history...
Loading reviews...