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.
Add the dependency to your pom.xml:
<dependency>
<groupId>com.github.adesigns</groupId>
<artifactId>csv-creator</artifactId>
<version>1.0.2</version>
</dependency>
Add the dependency to your build.gradle:
compile 'com.github.adesigns:csv-creator:1.0.2'
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;
}
}
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();