toCsv method

String toCsv({
  1. CsvConfig? config,
  2. bool formulasAsText = false,
})

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:

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);
}