Parse CSV or Excel files to DTO
(NOTE: This project is WIP, its API will change in the future)
Easily parse CSV/Excel files to DTOs
Suppose you have a csv file
value1,value2
value1,value2
value1,value2
...
Easily parse that to a DTO by annotating your DTO class with @JsonPropertyOrder
, which defines the order of field values to be used for parsing
@JsonPropertyOrder({"field1", "field2"})
class MyDTO {
private String field1;
private String field2;
// Getters and setters...
}
Instantiate CsvAsDTO<>()
and retrieve parsed csv as DTO
List<MyDTO> dtos = new CsvAsDTO<>(
csv,
MyDTO.class
).asDTOs();
csv
can be:
Stream<String>
where each String
is 1 row (i.e "value1,value2"
)Iterator<String>
where each String
also 1 rowBufferedReader
InputStreamReader
ByteArrayStreamReader
byte[]
Parsing excel files to DTO is the same with CSV:
List<MyDTO> dtos = new CsvAsDTO<>(
new ExcelIterator(
workbook
)
MyDTO.class
).asDTOs()
ExcelIterator
in an implementation of Iterator<String>
that run through all sheets and rows, and iteration of concatenated cells per row as String.
workbook
is an instance of XSSFWorkbook
from Apache POI
Parsing is comma separated by default. However you can annotate your
DTO class with @ColumnSeparator
and provide the delimiter
// Tab separated
@ColumnSeparator(value = '\t')
@JsonPropertyOrder({"field1", "field2"})
class MyDTO { }
// Colon separated
@ColumnSeparator(value = ':')
@JsonPropertyOrder({"field1", "field2"})
class MyDTO2 { }
Add the following on your pom.xml
<dependencies>
<dependency>
<groupId>com.dragonfruit</groupId>
<artifactId>Parseux</artifactId>
<version>0.0.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-csv</artifactId>
<version>2.9.0.pr3</version>
</dependency>
...
</dependencies>
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
Apache Version 2