toCsv method
Serialises this sheet's used cell range to CSV text.
Every row spans the sheet's used width (maxColumns); empty cells become
empty fields. Values map to CSV as follows:
- text stays text;
- IntCellValue / DoubleCellValue become numbers;
- BoolCellValue becomes
true/false; - DateCellValue becomes an ISO date (
2024-01-31); - DateTimeCellValue becomes an ISO date-time (
2024-01-31T09:30:00); - TimeCellValue becomes
HH:mm:ss; - a CellErrorValue becomes its literal (e.g.
#DIV/0!); - a FormulaCellValue becomes its cached result, or the formula text
prefixed with
=whenformulasAsTextistrueor there is no cached result.
Pass a config to change the delimiter, quoting, or line ending; the
default is standard RFC 4180 CSV. Presets like CsvConfig.excel and
CsvConfig.tsv are available.
final text = sheet.toCsv();
final tsv = sheet.toCsv(config: const CsvConfig.tsv());
Implementation
String toCsv({CsvConfig? config, bool formulasAsText = false}) {
final grid = <List<dynamic>>[
for (final row in rows)
[
for (final cell in row)
_scalarFromCell(cell?.value, formulasAsText: formulasAsText),
],
];
return CsvCodec(config ?? const CsvConfig()).encode(grid);
}