Sample code for 30+ languages & platforms
Dart

Create CSV File

See more CSV Examples

Demonstrates how to create a new CSV file with some simple content.

Chilkat Dart Downloads

Dart
import 'package:chilkat/chilkat.dart';

void main() {
  // This sample code creates a new CSV file (sample.csv)
  // that contains this content:
  // 
  // year,color,country,food
  // 2001,red,France,cheese
  // 2005,blue,"United States",hamburger

  final csv = CkCsv();

  // Indicate that the 1st row
  // should be treated as column names:
  csv.hasColumnNames = true;

  csv.setColumnName(0, 'year');
  csv.setColumnName(1, 'color');
  csv.setColumnName(2, 'country');
  csv.setColumnName(3, 'food');

  csv.setCell(0, 0, '2001');
  csv.setCell(0, 1, 'red');
  csv.setCell(0, 2, 'France');
  csv.setCell(0, 3, 'cheese');

  csv.setCell(1, 0, '2005');
  csv.setCell(1, 1, 'blue');
  csv.setCell(1, 2, 'United States');
  csv.setCell(1, 3, 'hamburger');

  // Write the CSV to a string and display:
  final csvDoc = csv.saveToString();
  print(csvDoc);

  // Save the CSV to a file:
  try {
    csv.saveFile('out.csv');
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
  }
}