Skip to content

A super-simple library to generate CSVs from POJOs.

Notifications You must be signed in to change notification settings

adesigns/csv-creator

Repository files navigation

CSV Creator

A super-simple library to generate CSVs from POJOs. Allows you to define the expected output of a CSV file by creating a POJO marked with annotations.

Build Status Code Coverage

Installation

via Maven

Add the dependency to your pom.xml:

<dependency>
  <groupId>com.github.adesigns</groupId>
  <artifactId>csv-creator</artifactId>
  <version>1.0.2</version>
</dependency>

via Gradle

Add the dependency to your build.gradle:

compile 'com.github.adesigns:csv-creator:1.0.2'

Basic Usage

1. Create a POJO that will represent your CSV:

package com.somecompany.csv.pojo;

import com.adesigns.csv.annotation.CsvColumn;
import java.util.Date;

public class CsvPojo {

    @CsvColumn(title = "String Column")
    private String stringColumn;

    @CsvColumn(title = "Integer Column")
    private Integer integerColumn;

    @CsvColumn(title = "Date Column", dateFormat="MM/dd/yyyy") // dateFormat is optional.  See CsvColumn for default format.
    private Date dateColumn;

    public String getStringColumn() {
        return stringColumn;
    }

    public void setStringColumn(String stringColumn) {
        this.stringColumn = stringColumn;
    }

    public Integer getIntegerColumn() {
        return integerColumn;
    }

    public void setIntegerColumn(Integer integerColumn) {
        this.integerColumn = integerColumn;
    }

    public Date getDateColumn() {
        return dateColumn;
    }

    public void setDateColumn(Date dateColumn) {
        this.dateColumn = dateColumn;
    }
}

2. Create some POJOs and Write to XML

Pojo firstPojo = new Pojo();
firstPojo.setStringColumn("String 1");
firstPojo.setIntegerColumn(1234);
firstPojo.setDateColumn(new Date());

Pojo secondPojo = new Pojo();
secondPojo.setStringColumn("String 2");
secondPojo.setDateColumn(new Date());

List<Pojo> pojoList = new LinkedList<Pojo>();
pojoList.add(firstPojo);
pojoList.add(secondPojo);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
Writer writer = new OutputStreamWriter(outputStream);

CsvCreator<Pojo> csvCreator = new CsvCreator<Pojo>(writer, Pojo.class);

csvCreator.write(pojoList);

writer.close();

byte[] csvOutput = outputStream.toByteArray();

About

A super-simple library to generate CSVs from POJOs.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages