Skip to content
Arnaud Roger edited this page Jul 2, 2015 · 2 revisions

Getting started

    // if you will reuse the same writer it is better to cache the dsl object
    CsvWriter.CsvWriterDSL<DbObject> dsl = CsvWriter.from(DbObject.class);
    try (FileWriter writer = new FileWriter("test.csv")) {
        CsvWriter<DbObject> csvWriter = dsl.to(writer);
        csvWriter.append(obj1);
    }

Manually specifying columns

    CsvWriter.CsvWriterDSL<DbObject> dsl = 
        CsvWriter
            .from(DbObject.class)
            .columns("id", "name", "email");
    try (FileWriter writer = new FileWriter("test.csv")) {
        CsvWriter<DbObject> csvWriter = dsl.to(writer);
        csvWriter.append(obj1);
    }

Adding formating

    CsvWriter.CsvWriterDSL<DbObject> dsl = 
        CsvWriter
            .from(DbObject.class)
            .columns("id", "name", "email")
            .column("creation_time", new SimpleDateFormat("dd/MM/yyyy"));
    try (FileWriter writer = new FileWriter("test.csv")) {
        CsvWriter<DbObject> csvWriter = dsl.to(writer);
        csvWriter.append(obj1);
    }
Clone this wiki locally