CSV import and export
Move between .xlsx and CSV or TSV in either direction.
Install#
dart pub add excel_plusimport 'package:excel_plus/excel_plus.dart';CSV to a workbook#
final excel = Excel.fromCsv('name,age\nAlice,30\nBob,25', sheetName: 'People');
File('people.xlsx').writeAsBytesSync(excel.save()!);Adding a CSV sheet to a workbook you already have#
excel.importCsv('a\tb\n1\t2', sheetName: 'Tabbed', config: const CsvConfig.tsv());A sheet back to CSV#
final csv = excel['People'].toCsv();Type inference will not damage your data#
Values that look numeric but are not, such as a zero padded id, stay text. 007 does not become 7. Pass inferTypes: false to keep every field as text.
Messy exports#
Real CSV files often carry a comment preamble or junk rows before the header.
excel.importCsv(
'# Sales report 2026\nname,total\nAlice,95\nBob,88',
sheetName: 'Sales',
config: const CsvConfig(comment: '#'),
);CsvConfig also takes skipRows to drop leading rows and maxRows to read only a slice, plus delimiter, quoting and line ending settings.
Forcing column types#
When guessing is not good enough, declare the columns. The first row is the header, each named column is coerced to its declared type, and a value that cannot convert throws CsvParseException.
excel.importCsv('id,score\n001,9\n002,8', sheetName: 'Scores', schema: const CsvSchema(
columns: [
CsvColumnDef(name: 'id', type: String),
CsvColumnDef(name: 'score', type: double),
],
));